PHP - 无法读取上传的临时文件
PHP - Cannot read uploaded temporary file
我在 VPS 上提供邮件服务,我的 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,使用 Nginx 1.6.2 和 PHP-FPM 5.6.7 来自 LaunchPad 的存储库。脚本在我的本地机器上运行正常,环境完全相同。
我认为问题是Nginx的某些配置或PHP引起的,但是查看配置文件后,我只能找到关于大小和时间限制的配置。
我现在真的很迷茫。请帮忙,谢谢。
更新
Apache 2.4 + PHP 在相同 VPS.
上正常工作
我终于自己解决了这个问题。感谢@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
被注释掉了。
我在 VPS 上提供邮件服务,我的 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,使用 Nginx 1.6.2 和 PHP-FPM 5.6.7 来自 LaunchPad 的存储库。脚本在我的本地机器上运行正常,环境完全相同。
我认为问题是Nginx的某些配置或PHP引起的,但是查看配置文件后,我只能找到关于大小和时间限制的配置。
我现在真的很迷茫。请帮忙,谢谢。
更新
Apache 2.4 + PHP 在相同 VPS.
上正常工作我终于自己解决了这个问题。感谢@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
被注释掉了。