Jump to content

fileupload help


doforumda

Recommended Posts

i want to upload file with php. my code is below. I describe my problem below the whole code

 

upload.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="uploadProcess.php" method="post" enctype="multipart/form-data">
<input type="file" name="myfile"  /><p>
    <input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>

 

uploadProcess.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
include("getExtension.php");
include("imageResizer.php");

$name = $_FILES['myfile']['name'];
$type = $_FILES['myfile']['type'];
$size = $_FILES['myfile']['size'];
$temp_name = $_FILES['myfile']['tmp_name'];
$error = $_FILES['myfile']['error'];

echo $temp_name."<br>";
echo $name."<br>";

$ext = getExtension($name);
$ext = strtolower($ext);
echo $ext."<br>";
//die();
if($error == 0) {
$location = "photos/".$name;
if($size <= 1000000) {
	if($ext == "png" || $ext == "jpeg" || $ext == "jpg" || $ext == "gif") {
		if(!file_exists($location)) {
			//move_uploaded_file($temp_name, $location);
			image($name,$ext);
			echo "File uploaded successfully.";
		}
		else
			die("This file is already exists.");
	}
	else
		die("This format is not supported. Only png, jpeg, jpg and gif images are supported.");
}
else
	die("File size is too big.");
}
else
die("There is an error uploading code: $error");

?>
</body>
</html>

 

imageResizer.php

<?php
function image($name, $ext) {
// The file
$filename = $name;
$percent = 0.05;
$save = "photos/".$name;

// Content type
header('Content-type: image/'.$ext);

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

if($width > 60 && $height > 60) {
	// Resample
	$image_p = imagecreatetruecolor($new_width, $new_height);
	$image = imagecreatefromjpeg($filename);
	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
}
else {
	$image_p = imagecreatetruecolor($width, $height);
	$image = imagecreatefromjpeg($filename);
	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
}
// Output
imagejpeg($image_p, $save, 100);
}
//image('test.jpg','jpg');
?>

 

here the problem is in upload Process.php when i call function image($name,$ext); the it does not save my file on the server but when i call this function like this image('image.jpg','jpg'); then it saves this file on the server. how can i fix this so it saves file when i pass variables as parameters?

Link to comment
https://forums.phpfreaks.com/topic/193426-fileupload-help/
Share on other sites

ok here is getExtension.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
function getExtension($str) {
 $i = strrpos($str,".");
 if (!$i) { 
 	return ""; 
 }
 $l = strlen($str) - $i;
 $ext = substr($str,$i+1,$l);
 return $ext;
}
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/193426-fileupload-help/#findComment-1018398
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.