PDA

View Full Version : Server Auto Deleting Uploaded Image


Osoisimo
06-14-2009, 07:24 AM
I have a PHP script that uploads an image. The script is fine; it clearly works as the image gets to the temporary directory. However, immediately after it finishes processing the upload to the temp dir, it deletes it, thereby ruining the rest of the script. The whole uploading portion is proven by a nicely timed screen shot of the tmp directory with the permissions page open. I have asked others and they have no idea how, or why its doing this.

Any help would be greatly appreciated, as after this, I can move onto final dev.

CFMaBiSmAd
06-14-2009, 08:40 AM
The temporary uploaded image is automatically deleted when the script that is the target of the upload form ends execution. If your script is redirecting or otherwise ending execution or experiences a fatal php runtime error, then yes the temporary uploaded image will be deleted.

What is your script and what if any php errors are being reported or logged?

It is also likely that the file will be deleted by php for some of the possible upload errors. What value does your error checking code show for the $_FILES ... ['error'] element?

Osoisimo
06-14-2009, 06:40 PM
Here is the form code:

<?php

session_start();

if($_GET['type'] == "team"){

$type = "team";

}

elseif($_GET['type'] == "user"){

$type = "user";

}

else{

$errormes = "<div align=\"center\" class=\"error\"><big>An error, there was. Try again, you will.</big><small> -- Master Rower Yoda</small></div>";

}

?>

<!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=iso-8859-1" />

<title>Rower's Log Image Upload Tool</title>

<link href="/includes/style.css" rel="stylesheet" type="text/css" />

</head>



<body>

<?php

if(!session_is_registered('userid')){

echo "<div align=\"center\"class=\"error\">In order to upload an image, please login.</div>";

}

else{

if(isset($errormes)){

echo "$errormes";

exit;

}

?>

<div align="center" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px;">

<br />

Simply browse for the photo on your computer and upload it. <br /> Please only submit photos that are relevant (eg. somebody rowing, the team rowing, a team photo, etc). <br /> In addition, please submit photos for which you have permission for. <br />

<strong>Size Maximum</strong>: 3Mb<br />

<strong>Supported File Types</strong>: jpg, gif, png

<form action="upload_image_process.php" method="post" enctype="multipart/form-data">

<input type="hidden" name="MAX_FILE_SIZE" value="3145728" />

<input type="hidden" name="type" value="<?php echo $type; ?>" />

<input type="file" name="photo" />

<input type="submit" value="Upload Image" />

</form>

</div>

<? } ?>

</body>

</html>



Here is the processing code:

<?php

session_start();

$userid = $_SESSION['userid'];

$file = $_FILES['photo'];



if(!$file){

$errormes = "<div class=\"error\">No file was uploaded...</div>";

}

$name = $file['tmp_name'];



if($file['error'] > 0){

$errormes = "Chances are, we messed something up. Please use the contact form and tell us that: $file[error] is out of wack.";

}



$curtimestamp = time();



$newname = $userid;

$newname .= $curtimestamp;

$newname = md5($newname);

if($file['type'] == "image/jpeg"){

$end = "jpg";

}

elseif($file['type'] == "image/png"){

$end = "png";

}

elseif($file['type'] == "image/gif"){

$end = "png";

}



$success = move_uploaded_file("$_FILES[photo][tmp_name]", "images/uploaded/$newname.$end");

//$chown = chown("/var/www/images/uploaded/$newname.$end", "root");

$chmod = chmod("images/uploaded/$newname.$end", 0755);

if($success AND $chmod){

$errormes = "Nice Picture!";

}

else{

$errormes = "The upload failed (epically I might add). You might try again, or drop us a line, and will help you.<br />";

print_r($file);

}

?>

<!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=iso-8859-1" />

<title>Rower's Log Image Upload Tool</title>

<link href="/includes/style.css" rel="stylesheet" type="text/css" />

</head>



<body>

<?php

if(!session_is_registered('userid')){

echo "<div align=\"center\"class=\"error\">In order to upload an image, please login.</div>";

}



if(isset($errormes)){

echo "$errormes";

exit;

}

?>

</body>

</html>



I have print_r the file as you can see, and according to it, 'error'=0.

Spefically, this is the exact output I get:

Warning: chmod() [function.chmod]: No such file or directory in /var/www/upload_image_process.php on line 32
Array ( [name] => Yarn rainbow edited.jpg [type] => image/jpeg [tmp_name] => /var/www/temp/php0qBkcu [error] => 0 [size] => 979120 ) The upload failed (epically I might add). You might try again, or drop us a line, and will help you.

CFMaBiSmAd
06-14-2009, 06:52 PM
Notice errors are being hidden by your error_reporting setting and there is likely an error that explains why the code is not working. Add the following two lines immediately after your first opening <?php tag -

ini_set("display_errors", "1");
error_reporting(E_ALL);

Osoisimo
06-14-2009, 06:59 PM
Notice errors are being hidden by your error_reporting setting and there is likely an error that explains why the code is not working. Add the following two lines immediately after your first opening <?php tag -

ini_set("display_errors", "1");
error_reporting(E_ALL);

I did that and got no error. I did change one thing though, and that was the move_uploaded_file() from $_FILES[photo][tmp_name], to $files[tmp_name]. And it worked too. Finally. Thanks for your help.