You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP上传返回UPLOAD_ERR_OK但临时目录无文件的排查求助

文件上传问题:Windows正常但Linux/Apache下临时目录无文件

我用HTML表单向服务器上传文件,代码在Windows的XAMPP环境中运行正常(文件可上传至临时目录并移至目标目录),但部署到Linux/Apache服务器后,返回UPLOAD_ERR_OK状态,临时目录却找不到文件。

form.html

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
  Select image to upload:
  <input type="file" name="fileToUpload" id="fileToUpload">
  <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

upload.php(来自w3school示例)

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

echo json_encode($_FILES);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}

// Check if file already exists
if (file_exists($target_file)) {
  echo "Sorry, file already exists.";
  $uploadOk = 0;
}

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}
?>

我认为问题不在脚本本身,而是目录权限问题。

相关配置信息

php.ini配置

file_uploads = On
;upload_tmp_dir = 
upload_max_filesize = 2M
max_file_uploads = 20

$_FILES输出结果

执行echo json_encode($_FILES);输出:

{"fileupload":
      {"name":"rectify2222.zip",
       "type":"application/x-zip-compressed",
       "tmp_name":"/tmp/phpaXKMXW",
       "error":0,
       "size":540941
      }
}

phpinfo()查看的配置

DirectiveLocal ValueMaster Value
upload_tmp_dir/tmp/tmp

临时目录属性

项值
directory/tmp
Grouproot(0)
Ownerroot(0)
Permission1777

已尝试的操作

  • 修改文件夹权限:尝试将文件夹权限改为0755、0777、1777、7777(含递归),无效
  • 修改目录所有者:执行chown www-data:www-data /tmp,无效
  • 修改目录组:执行以下命令,无效
    sudo groupadd newgroup
    usermod -a -G newgroup root
    usermod -a -G newgroup www-data
    usermod -a -G newgroup nobody
    chgrp -R newgroup /tmp
    chmod -R 755 myfiles
    
  • 创建新目录并修改上传临时目录:创建权限为0777的新目录/tmp2,修改php.ini中的upload_tmp_dir =/tmp2,运行脚本后文件仍未出现在该目录中

请问还有什么操作或配置需要调整?我是初学者,希望能得到帮助。

内容的提问来源于stack exchange,提问作者Brad Newers

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 05:40:29