Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php chmod() not changing permissions

I am having problems with a picture uploading script.

I know there are hundreds of the same questions, but I haven't found the one that would be work for me.

$upload_dir = "images/postcards/";
chmod($upload_dir, 777);
if (is_writable($upload_dir)) {
    echo 'The file is writable';
} else {
    echo 'The file is not writable';
}

This always returns that the file is "not writable"

I tried setting chmod to 0777 and -rwxrwxrwx. But result was always the same. Any Ideas?

like image 389
Sebastjan Avatar asked Mar 02 '13 01:03

Sebastjan


2 Answers

The directory must be owned by the user invoking the script (typically www-data, apache or httpd if you're running the script in a apache/*NIX setup). A user can't set 777 permissions on directories it doesn't own.

See the note on the chmod() manual:

The current user is the user under which PHP runs. It is probably not the same user you use for normal shell or FTP access. The mode can be changed only by user who owns the file on most systems.

like image 187
jman Avatar answered Sep 24 '22 14:09

jman


First , open PHP error_report by adding two line on top of your code, see if there is a error coming from chmod:

ini_set('display_errors', true);
error_reporting(E_ALL);

Make sure your WebServer has the permission to that directory, my guess is the WebServer don't have permission.

like image 39
CharlieJade Avatar answered Sep 26 '22 14:09

CharlieJade