lundi 20 février 2017

PHP cannot write to folder wither permission 777

I've just installed php on my Fedora 25 while I find I can't do file upload with it.

My php code is like this

<?php
$upload_root = "/var/www/html/upload/";
// echo isset($_FILES["file"]);
// echo $_FILES["file"]["tmp_name"];
$upload_path = $upload_root . basename($_FILES["file"]["name"]);
$done_upload = 1;
$file_ext = pathinfo($upload_path,PATHINFO_EXTENSION);

echo $upload_path;
echo "<br/>";

if (file_exists($_FILES["file"]["tmp_name"])) {
    echo "Upload success<br/>";
    echo filesize($_FILES["file"]["tmp_name"]);
    echo "<br/>";
} else {
    echo "No such uploaded file";
}

if (file_exists($upload_path)) {
    echo "File exists<br/>";
    $done_upload = 0;
} else {
    copy($_FILES["file"]["tmp_name"], $upload_path);
    if (!move_uploaded_file($_FILES["file"]["tmp_name"], $upload_path)) {
        echo "Fail to move.<br/>";
    } else {
        echo "Successful uploaded.<br/>";
    }
}
echo var_dump($_FILES['file']);
?>

I find I cannot actually upload the file because of permission denied and I already changed the folder's permission to 777 and owner to apache

[root@wifi-roaming-128-4-63-102 html]# ll
total 16
-rwxrwxrwx. 1 lisanhu lisanhu  395 Feb 20 10:12 index.html
-rwxrwxrwx. 1 lisanhu lisanhu  229 Feb 20 09:40 index.php
drwxrwxrwx. 2 apache  apache  4096 Feb 20 10:14 upload
-rwxrwxrwx. 1 lisanhu lisanhu  832 Feb 20 13:13 upload.php
[root@wifi-roaming-128-4-63-102 html]# pwd
/var/www/html
[root@wifi-roaming-128-4-63-102 html]# 

The output from my browser looks like this

/var/www/html/upload/test
Upload success
21
Fail to move.
array(5) { ["name"]=> string(4) "test" ["type"]=> string(24) "application/octet-stream" ["tmp_name"]=> string(14) "/tmp/phpmGWuay" ["error"]=> int(0) ["size"]=> int(21) } 

The error log

[root@wifi-roaming-128-4-63-102 html]# tail /var/log/httpd/error_log 
[Mon Feb 20 13:07:00.867237 2017] [:error] [pid 2294] [client ::1:40360] PHP Warning:  move_uploaded_file(): Unable to move '/tmp/phpm2OyFL' to '/var/www/html/uploadtest' in /var/www/html/upload.php on line 26, referer: http://localhost/
[Mon Feb 20 13:07:01.611295 2017] [:error] [pid 2294] [client ::1:40360] PHP Warning:  copy(/var/www/html/uploadtest): failed to open stream: Permission denied in /var/www/html/upload.php on line 25, referer: http://localhost/
[Mon Feb 20 13:07:01.611407 2017] [:error] [pid 2294] [client ::1:40360] PHP Warning:  move_uploaded_file(/var/www/html/uploadtest): failed to open stream: Permission denied in /var/www/html/upload.php on line 26, referer: http://localhost/
[Mon Feb 20 13:07:01.611448 2017] [:error] [pid 2294] [client ::1:40360] PHP Warning:  move_uploaded_file(): Unable to move '/tmp/phpOKFS3O' to '/var/www/html/uploadtest' in /var/www/html/upload.php on line 26, referer: http://localhost/
[Mon Feb 20 13:16:38.902061 2017] [:error] [pid 2610] [client ::1:40926] PHP Warning:  copy(/var/www/html/upload/test): failed to open stream: Permission denied in /var/www/html/upload.php on line 24, referer: http://localhost/
[Mon Feb 20 13:16:38.902112 2017] [:error] [pid 2610] [client ::1:40926] PHP Warning:  move_uploaded_file(/var/www/html/upload/test): failed to open stream: Permission denied in /var/www/html/upload.php on line 25, referer: http://localhost/
[Mon Feb 20 13:16:38.902120 2017] [:error] [pid 2610] [client ::1:40926] PHP Warning:  move_uploaded_file(): Unable to move '/tmp/phpjUZz2S' to '/var/www/html/upload/test' in /var/www/html/upload.php on line 25, referer: http://localhost/
[Mon Feb 20 13:16:40.733010 2017] [:error] [pid 2610] [client ::1:40926] PHP Warning:  copy(/var/www/html/upload/test): failed to open stream: Permission denied in /var/www/html/upload.php on line 24, referer: http://localhost/
[Mon Feb 20 13:16:40.733177 2017] [:error] [pid 2610] [client ::1:40926] PHP Warning:  move_uploaded_file(/var/www/html/upload/test): failed to open stream: Permission denied in /var/www/html/upload.php on line 25, referer: http://localhost/
[Mon Feb 20 13:16:40.733217 2017] [:error] [pid 2610] [client ::1:40926] PHP Warning:  move_uploaded_file(): Unable to move '/tmp/phpmGWuay' to '/var/www/html/upload/test' in /var/www/html/upload.php on line 25, referer: http://localhost/
[root@wifi-roaming-128-4-63-102 html]# 

Would you please help me with it? I'll really appreciate it.




Aucun commentaire:

Enregistrer un commentaire