Php 图像上载到临时文件夹警告

Php 图像上载到临时文件夹警告,php,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 n

从下面的脚本中,有人能告诉我在脚本输出时得到警告消息的错误吗?上载脚本为-

代码:

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

这是第37行吗

<?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
unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder

当您使用move_upload_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();
  }

您在这里使用的正则表达式

preg_match("/.(gif|jpg|png)$/i", $fileName)
可能是错的。因为它将返回true,即使是对于此文件名
$fileName=“adjGIF”
,我希望您不要这样做。因此请使用此选项

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

注意:-尽管这不是答案,但它将使您的代码正确。

使用以下代码将图像文件移动到名为avatar的文件夹中:

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

//如果文件名指定的文件或目录存在且可读,则返回TRUE,否则返回FALSE

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

确切地正如函数名所示,它实际上将文件从tmp文件夹移动到提供的目标。最后,您不必担心以后会删除tmp文件,我明白其中的逻辑。我删除了第37行,这似乎解决了问题谢谢,这会有帮助