图像上传到临时文件夹警告

时间:2013-12-24 12:14:16

标签: php

从下面的脚本中,有人能告诉我在脚本输出时收到警告信息我做错了什么吗?上传脚本是 -

代码:

<?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";
?> 

<小时/> 我的表单是

<?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>';
?>


输出是

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


第37行是

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

4 个答案:

答案 0 :(得分:1)

当您使用move_uploaded_file命令时,tmp位置中的文件不再存在,因此无法删除,我会说。

在您的代码中看一点,考虑重组:

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();
  }

答案 1 :(得分:0)

您在此处使用的regEx

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

可能错误。因为它会返回true ,即使对于这个文件名$fileName="adjGIF"也是如此,我希望你不要这样做。所以改为使用这个

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

注意: - 即使不是答案,但它会使您的代码更正。

答案 2 :(得分:0)

使用下面的代码将图像文件移动到名为文件夹的头像:

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

答案 3 :(得分:0)

如果filename指定的文件或目录存在且可读,则

//返回TRUE,否则返回FALSE。

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