onejed
onejed

Reputation: 31

Image upload to Temp Folder warning

From the script below, can anyone tell me what I've done wrong to get the warning message on output of the script? The upload script is -

Code:

<?php
// Access the $_FILES global variable for this specific file being uploaded
// and create local PHP variables from the $_FILES array of information
$fileName = $_FILES["thumb"]["name"]; // The file name
$fileTmpLoc = $_FILES["thumb"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["thumb"]["type"]; // The type of file it is
$fileSize = $_FILES["thumb"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["thumb"]["error"]; // 0 = false | 1 = true
$fileSplit = explode(".", $fileName); // Split file name into an array using the dot
$fileExt = end($fileSplit); // Now target the last array element to get the file extension
// START PHP Image Upload Error Handling --------------------------------------------------
if (!$fileTmpLoc) { // if file not chosen
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
} else if($fileSize > 5242880) { // if file size is larger than 5 Megabytes
    echo "ERROR: Your file was larger than 5 Megabytes in size.";
    unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
    exit();
} else if (!preg_match("/.(gif|jpg|png)$/i", $fileName) ) {
     // This condition is only if you wish to allow uploading of specific file types    
     echo "ERROR: Your image was not .gif, .jpg, or .png.";
     unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
     exit();
} else if ($fileErrorMsg == 1) { // if file upload error key is equal to 1
    echo "ERROR: An error occured while processing the file. Try again.";
    exit();
}
// END PHP Image Upload Error Handling ----------------------------------------------------
// Place it into your "Avatars" folder mow using the move_uploaded_file() function
$moveResult = move_uploaded_file($fileTmpLoc, "Avatars/$fileName");
// Check to make sure the move result is true before continuing
if ($moveResult != true) {
    echo "ERROR: File not uploaded. Try again.";
    unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
    exit();
}
unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
// Display things to the page so you can see what is happening for testing purposes
echo "The file named <strong>$fileName</strong> uploaded successfully.<br /><br />";
echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
echo "It is an <strong>$fileType</strong> type of file.<br /><br />";
echo "The file extension is <strong>$fileExt</strong><br /><br />";
echo "The Error Message output for this upload is: $fileErrorMsg";
?> 


My form is this

<?php
    $profile_pic_btn = '<a href="#" onclick="return false;" onmousedown="toggleElement(\'avatar_form\')">Toggle Avatar Form</a>';
    $avatar_form  = '<form id="avatar_form" enctype="multipart/form-data" method="POST" action="process_reguser_exec.php">';
    $avatar_form .=   '<h4>Change your avatar</h4>';
    $avatar_form .=   '<input type="file" name="thumb">';
    $avatar_form .=   '<p><input type="submit" value="Upload"></p>';
    $avatar_form .= '</form>';
?>


The output is this

Warning: unlink(C:\xampp\tmp\php8E40.tmp): No such file or directory in C:\xampp\htdocs\MyWebSite\process_reguser_exec.php on line 37

The file named image1.JPG uploaded successfully.

It is 3337452 bytes in size.

It is an image/jpeg type of file.

The file extension is JPG

The Error Message output for this upload is: 0


Line 37 is this

unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder

Upvotes: 0

Views: 869

Answers (4)

pradeep
pradeep

Reputation: 11

use below code to move image file in to avatar named folder :

move_uploaded_file($fileTmpLoc,"Avatars".$fileName);

Upvotes: 0

Nanhe Kumar
Nanhe Kumar

Reputation: 16307

//returns TRUE if the file or directory specified by filename exists and is readable, FALSE otherwise.

if (is_readable($fileTmpLoc)) {
   unlink($fileTmpLoc);
}

Upvotes: 0

Let me see
Let me see

Reputation: 5094

The regEx that you are using here

preg_match("/.(gif|jpg|png)$/i", $fileName)

is probably wrong. Because it will return true even for this file name $fileName="adjGIF" and i hope that you do not want this.So instead use this

preg_match("/.(\.(gif|jpg|png))$/i", $fileName)

Note:- Even though its not the answer but it will make your code correct.

Upvotes: 0

Jelle Ferwerda
Jelle Ferwerda

Reputation: 1229

When you have used the move_uploaded_file command, the file in the tmp location is no longer there, and therefor cannot be removed, I would say.

Looking a bit harder at your code, consider a restructuring:

if(move_uploaded_file($fileTmpLoc, "Avatars/$fileName"))
  {
  // do the image stuff
  }
else
  {
  echo "ERROR: An error occured uploading and storing your file. Please try again.";
  // Add a test to see whether the file exists
  unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
  exit();
  }

Upvotes: 1

Related Questions