【问题标题】:is_uploaded_file function worked in linux But not in Windowsis_uploaded_file 函数在 linux 中有效,但在 Windows 中无效
【发布时间】:2020-04-14 13:20:19
【问题描述】:

代码

if(is_array($_FILES) && isset($_FILES['photography_attachment'])) {
      if(is_uploaded_file($_FILES['photography_attachment']['tmp_name'])) {
        $fileName = $_FILES["photography_attachment"]["name"]; // The file name
        $fileTmpLoc = $_FILES["photography_attachment"]["tmp_name"]; // File in the PHP tmp folder
        $fileType = $_FILES["photography_attachment"]["type"]; // The type of file it is
        $fileSize = $_FILES["photography_attachment"]["size"]; // File size in bytes
        $fileErrorMsg = $_FILES["photography_attachment"]["error"]; // 0 = false | 1 = true
        $kaboom = explode(".", $fileName); // Split file name into an array using the dot
        $fileExt = end($kaboom); // Now target the last array element to get the file extension

        if (!$fileTmpLoc) { // if file not chosen
          $error = $error."<p>Please browse for a file before clicking the upload button.</p>";
        } else if($fileSize > 10485760) { // if file size is larger than 2 Megabytes
          $error = $error."<p><span>Your file was larger than</span> 10 <span>Megabytes in size</span>.</p>";
          unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
        } else if (!preg_match("/.(gif|jpg|png|jpeg)$/i", $fileName) ) {
          // This condition is only if you wish to allow uploading of specific file types
          $error = $error."<p>Your file was not .gif, .jpg, .png</p>";
          unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
        } else if ($fileErrorMsg == 1) { // if file upload error key is equal to 1
          $error = $error."<p>An error occured while processing the file. Try again.</p>";
        }
     }else{ $error = "Please try again !!!"; }
  }else{ $error = "Attachment field cannot be blank!"; }

总是转到“请再试一次!!!”否则在 Windows 中上传图像时,但在 linux 系统中运行良好。

你能请任何人帮我解决这个问题吗?

【问题讨论】:

  • 如果逻辑成功,上面的代码对上传的文件不做任何事情?!
  • $_FILES response => [photography_attachment] => Array ([name] => ttest.jpg [type] => [tmp_name] => [error] => 1 [size] => 0 )
  • @AD7six 我使用以下答案(理查德答案)解决了我的问题
  • @BhAvikGajjar 很酷(如果是真的,这似乎不太可能) - 对您在 cmets 中粘贴不同且不相关的错误没有帮助。

标签: php ajax file-upload


【解决方案1】:

在 Windows 平台上,您必须将文件路径中的“\”替换为“/”

像这样:

$file = str_replace ("\\", "/", $_FILES['photography_attachment']['tmp_name']);
if(is_uploaded_file($file)) {
   [...]
}

或使用 php 内置方法,适用于所有系统:

$file = realpath($_FILES['photography_attachment']['tmp_name']);
if(is_uploaded_file($file)) {
   [...]
}

【讨论】:

    猜你喜欢
    • 2014-10-23
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 2020-05-14
    相关资源
    最近更新 更多