【问题标题】:move_uploaded_file returns false but $_FILES error is 0move_uploaded_file 返回 false 但 $_FILES 错误为 0
【发布时间】:2019-06-09 15:56:19
【问题描述】:

我正在尝试在 Linux 机器上部署一个使用 CodeIgniter 框架构建的 PHP 项目。

由于某种原因,无法将上传的文件从临时目录移动到指定目录。

信息

  • PHP 7.2.19
  • CentOS 7
  • CodeIgniter 3.0.6

注意事项

  • 我已检查目标包含目录是否存在(不存在与待移动文件同名的重复文件)。
  • 我已经检查了目录的权限,应该没问题(我将组设置为apache,权限=所有者用户)
  • 检查ACL,与普通权限一致。
  • $_FILES['file']['error'] = 0(在调用move_uploaded_file之前和之后)
  • file_exists$_FILES['file']['tmp_name'] 返回 true
  • is_uploaded_file$_FILES['file']['tmp_name'] 返回 true
  • open_basedir 未设置
  • 上传的文件小于帖子和文件上传限制。

最后要提一下,振作起来,存在该站点的另一个副本(部署在具有不同目录名称的同一服务器上)并且上传工作正常。比较两个目录以捕获不同但什么也没找到(除了一些带有一些更新的视图,与配置无关,.htacess 任何东西)。

我应该看什么线索?

更新 1

代码:

    $createdFileName = NULL;
    $files = $_FILES;
    if (count($files) > 0) {
        $createdFileName = $this->GUID();
        $target_file = './upload/' . $createdFileName;
        $path_info = pathinfo($files["file"]["name"]);
        if (isset($path_info['extension'])) {
                $ext = $path_info['extension'];
        } else /*if (!$ext)*/ {
            $ext = substr($files['file']['type'], strrpos($files['file']['type'], '/') + 1);
        }
        $success = move_uploaded_file($files["file"]["tmp_name"], $target_file);
        if (!$success) {
            $result = json_encode(array('result' => FALSE, 'message' => 'Couldn\'t upload image file'));
        }
    }

这是与其他项目一起使用的代码,而不是与我正在处理的项目一起使用的代码。

我通过远程调试项目了解了error和其他人的值。

更新 2

以下是上传目录的权限。

drwxrwxr-x. 2 demoroot apache 4.0K Jun  9 14:06 upload/

以下是getfacl的输出:

# file: upload/
# owner: demoroot
# group: apache
user::rwx
group::rwx
other::r-x

【问题讨论】:

  • 比其他几百个副本稍微多一些努力,但仍然缺少代码 + 输入示例、目录结构、strace 或 IDE 调试。
  • How can I get useful error messages in PHP? 的 PD(m_u_f 不只是默默地返回 false)
  • @mario,添加了代码 sn-p 和权限值。
  • @mario,错误标志设置为报告(如果有)(error_reporting = -1,display_errors = 1)。响应中没有任何内容作为错误返回。有没有办法手动检查错误?
  • @mario,抛出错误move_uploaded_file failed to open stream permission denied。有什么想法可能来自哪里?

标签: php linux file-upload upload


【解决方案1】:

解决方案是运行以​​下命令

chcon -R --type httpd_sys_rw_content_t /path/to/upload/directory

我完全不知道这是做什么的,为什么我必须在 getfacl 输出似乎没问题的情况下运行它而不运行命令。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    相关资源
    最近更新 更多