Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi,

 

I have a question for uploading image part in add_print.php. Usually you would get rid of temporary file on the server after moving the file to the permanent location ( ex) ../uploads/$_FILES['image']['name'] ). However, in "add_print.php" file, it shows you delete the original file (the code like this below:)

 

 

// Delete the uploaded file if it still exists:

if ( isset($temp) && file_exists ($temp) && is_file($temp) ) {

unlink ($temp);

}

 

If you go up to the beginning part of this code, you will see :

 

 

// Check for an image:

if (is_uploaded_file ($_FILES['image']['tmp_name'])) {

 

// Create a temporary file name:

$temp = '../uploads/' . md5($_FILES['image']['name']);

 

// Move the file over:

if (move_uploaded_file($_FILES['image']['tmp_name'], $temp)) {

 

echo '<p>The file has been uploaded!</p>';

 

// Set the $i variable to the image's name:

$i = $_FILES['image']['name'];

 

} else { // Couldn't move the file over.

$errors[] = 'The file could not be moved.';

$temp = $_FILES['image']['tmp_name'];

}

 

} else { // No uploaded file.

$errors[] = 'No file was uploaded.';

$temp = NULL;

}

 

When the file is uploaded, $temp means the original file that is pointing to the permanent location.($temp = '../uploads/' . md5($_FILES['image']['name']);

 

I thought you only delete the temporary file after moving the file to the permanent. Why do you delete the original file? Also Why do you not delete the temporary file after the file is moved to the permanent location?

 

It would be appreciated if you can give me some explanation.

 

Thanks.

Link to comment
Share on other sites

You've got the logic backwards. When things work properly, the temporary file gets moved to its final destination and it does not need to be deleted. When things don't work properly, the temporary file still exists on the server and needs to be removed so as not to clutter things up. This is a complicated script, but if you reread the instructions for it, this may be more clear.

Link to comment
Share on other sites

 Share

×
×
  • Create New...