PHP - 无法读取上传的临时文件

时间:2015-03-27 15:33:35

标签: nginx php

我正在我的VPS上持有邮件服务,该服务由Roundcube提供网络界面

但我最近发现我的Roundcube无法正确上传联系人的头像(这是我第一次尝试上传头像)。所以我看了一下代码,发现它在读取上传的临时图片文件的属性时失败了。

为了弄清楚它是否是由Roundcube引起的,我写了一个简单的脚本。

<html>
<body>
<form method="post" action="/upload.php" enctype="multipart/form-data">
        <input type="file" name="test">
        <button type="submit">Upload</button>
</form>
</body>
</html>

<?php
var_dump($_FILES);
var_dump(file_exists($_FILES['test']['tmp_name']));

// read the properties by GD, as what Roundcube does
$props = getimagesize($_FILES['test']['tmp_name']);

var_dump($props);

输出结果为:

array(1) {
    ["test"]=> array(5) {
        ["name"]=> string(23) "kanakurayui_2_small.png"
        ["type"]=> string(9) "image/png"
        ["tmp_name"]=> string(14) "/tmp/php7OxqPq"
        ["error"]=> int(0)
        ["size"]=> int(49294) 
    }
}
bool(false)
bool(false)

看起来文件在上传后立即被删除,但我可以调用move_uploaded_file()将临时文件移动到另一个目录(这样邮件附件就能正常运行)。

我正在运行Ubuntu 14.04,其中包含来自LaunchPad存储库的Nginx 1.6.2和PHP-FPM 5.6.7。并且脚本在我的本地机器上正常工作,其环境完全相同。

我认为问题是由Nginx或PHP的某些配置引起的,但在检查配置文件后,我只能找到有关大小和时间限制的配置。

我现在真的很困惑。请帮助,谢谢。

更新

Apache 2.4 + PHP在同一个VPS上正常运行。

1 个答案:

答案 0 :(得分:1)

我终于自己解决了这个问题。感谢@insanebits提醒。

我查看了Nginx日志,发现了以下消息:

PHP message: PHP Warning:  getimagesize(): open_basedir restriction in effect. File(/tmp/php1PACw4) is not within the allowed path(s): (/var/www) in /var/www/test/upload.php on line 5

所以我将临时目录移动到/ tmp / php并将/ tmp / php添加到php.ini中的open_basedir设置

它在我的本地计算机上工作的原因是我的本地PHP配置是开发的副本,而open_basedir已被注释掉。

相关问题