Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

php - Upload file permission

I have PHP code like this:

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

$error = array();
if (isset($_POST['submit'])) {
    $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'upload';
    if (!empty($_FILES)) {
        $tmp = $_FILES['file']['tmp_name'];
        $file = $dir . DIRECTORY_SEPARATOR . $_FILES['file']['name'];
        if (is_uploaded_file($tmp)) {
            if (!chown($dir, 'nobody')) {
                $error[] = 'Owner tidak bisa di replace!';
            }
            if (!chmod($dir, intval(755, 8))) {
                $error[] = 'Direktori "' . $dir . '" tidak bisa diberi akses!';
            }
            if (!move_uploaded_file($tmp, $file)) {
                $error[] = 'Gagal memindahkan berkas "' . $file . '"';
            }
            if (file_exists($file)) {
                $error[] = 'Berhasil di unggah! ' . $file;
            }
        } else {
            $error[] = 'Berkas tidak bisa di unggah.';
        }
    }
}
?>

When running this code the following messages appear:

Warning: chown(): Operation not permitted in /var/www/html/jdih/upload.php on line 23

Warning: chmod(): Operation not permitted in /var/www/html/jdih/upload.php on line 24

Warning: move_uploaded_file(/var/www/html/jdih/upload/06-naiveBayes-example.pdf): failed to open stream: Permission denied in /var/www/html/jdih/upload.php on line 25

Warning: move_uploaded_file(): Unable to move '/tmp/phpMHL5CQ' to '/var/www/html/jdih/upload/06-naiveBayes-example.pdf' in /var/www/html/jdih/upload.php on line 25

What's going on here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should verify file permissions and file owner.

To change file permissions :

chmod -R 775 /var/www/html/jdih

And set the owner to the user who run your apache server.

chown www-data:www-data /var/www/html/jdih

Note : the owner depends on your distribution, on Ubuntu it's www-data, it could be httpd or http or something else.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...