php chmod() not changing permissions
Asked Answered
D

4

9

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?

Distillery answered 2/3, 2013 at 1:38 Comment(1)
in case of decimal 777 it is incorrect. you should always prefix with 0 check here chmod.Chipper
P
13

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.

Prothrombin answered 2/3, 2013 at 1:40 Comment(0)
C
2

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.

Coaster answered 2/3, 2013 at 1:58 Comment(1)
omg thanks so much for the error reporting thing. I was bashing my head against the wall trying to install stuff like Chrome Logger to get error reporting for php.Capparidaceous
Q
0

I already had the same problem you can change the file's permission by this code :

<?php
$ftp_details['ftp_user_name'] = 'your ftp username';
$ftp_details['ftp_user_pass'] = 'your ftp password';
$ftp_details['ftp_root'] = '/public_html/';
$ftp_details['ftp_server'] = 'ftp' . $_SERVER['HTTP_HOST'];
function ftp_chmod($path, $mod, $ftp_details) {
  extract($ftp_details);
  $conn_id = ftp_connect($ftp_server);
  $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to chmod $path directory
  if (ftp_site($conn_id, 'CHMOD ' . $mod . ' ' . $ftp_root . $path) !== false) {
    $success = true;
  }
  else {
    $success = false;
  }

  ftp_close($conn_id);
  return $success;
}
?>

I didn't run this code but I think it's Ok and it will help you. tell me if your problems resolved.

Quiles answered 10/7, 2013 at 16:31 Comment(0)
F
0

I was having similar troubles using chmod, although the file was owned by apache:apache (webserver user). In my case SELinux was getting in the way, disabling it made this clear:

sudo setenforce 0

And the chmod works. Now on to figuring out how to make a SELinux exception for this case... (and don't forget to enable SELinux, of course)

Fright answered 15/7, 2014 at 8:23 Comment(2)
Improve your answer to make it more comprehensive.Toon
This is the best answer for my specific case as i had the correct user and group set for the file. Also on the theme of server configurations, i also found that setting a new umask for my webserver was the best option: serverfault.com/a/384922/185510Chairman

© 2022 - 2024 — McMap. All rights reserved.