dsj0312 2015-04-10 22:33
浏览 32

PHP move_uploaded_file“无法移动”

Apologies if this has been answered elsewhere and I didn't make the correlation to my failures in posting to move_upload_file(). move_upload_file() is responding with 2 errors, "failed to open stream" and "unable to move". I'm trying to keep this as simple as possible for testing purposes only (since it doesn't work). This site is not exposed to the internet nor is used for production purposes, but rather my learning. A preemptive thank you for your patience and helpful tips on my troubles.

HTML form post:

<!DOCTYPE html>
<html>
<body>

<form enctype="multipart/form-data" action="recv.php" method="POST">
  <!-- MAX_FILE_SIZE must precede the file input field -->
  <input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
  <!-- Name of input element determines name in $_FILES array -->
  Send this file: <input name="userfile" type="file" />
  <input type="submit" value="Send File" />
</form>

</body>
</html>

recv.php:

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

$uploaddir = '/var/www/html/Pictures/';
//$uploaddir = 'Pictures/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo "File: $uploadfile<br>
";

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  echo "File is valid, and was successfully uploaded.
";
}

echo 'print_r($_FILES):';
print_r($_FILES);

print "</pre>";

?>

The result:

File: /var/www/html/Pictures/test.jpg

Warning:  move_uploaded_file(/var/www/html/Pictures/test.jpg): failed to open stream: Permission denied in /var/www/html/recv.php on line 13

Warning:  move_uploaded_file(): Unable to move '/tmp/phpuA2sGA' to '/var/www/html/Pictures/test.jpg' in /var/www/html/recv.php on line 13

print_r($_FILES):
Array
(
    [userfile] => Array
        (
            [name] => test.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/phpuA2sGA
            [error] => 0
            [size] => 43434
        )

)

And the correlated security (or lack thereof):

drwxrwxrwt. 8 root   root   4096 Apr 10 16:00 /tmp
drwxrwxrwt. 3 root   root     35 Apr 10 16:00 /var/tmp/
drwxrwxrwt. 2 apache apache    6 Apr 10 16:53 /var/www/html/Pictures/

Probably the most important note that I cannot correlate is that this originally did work at one time but I had erased the contents of the html folder. I noticed a linked file in Pictures that pointed back to /var/www/html. Carelessly I removed that link and subsequently the few files I had in /var/www/html for testing. I've rebuilt those couple files per the above specs but now receive the warnings from the attempted move. As the recv.php file shows, I've tried both the absolute path and the relative path. I realize a lot of this is considered against best practice, but I need to understand what is broken before addressing all the other concepts. Getenforce is still "Enforcing", if that matters.

  • 写回答

2条回答 默认 最新

  • dougao2830 2015-04-13 16:45
    关注

    It was an selinux permission issue. chcon -R -t httpd_sys_rw_content_t Pictures solved the problem.

    I realized that the 777 permissions were not the issue when printing the results of is_writable('Pictures'), which wound up false. A real quick test after setenforce 0 proved that I needed to address selinux.

    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路