2

I want to change the chmod values of the files from the user. But it does not work. My code is;

$chmod = "0777";
chmod($filename, $chmod);

I am entering chmod 777. But the chmod value of the file is 1411. I tried chmod 0777, 777, 00777. The results remain the same.

Mike Bovenlander
  • 5,236
  • 5
  • 28
  • 47
  • 1
    The most common reason this doesn't work is your webserver is running with user and group permissions that don't allow it to change the file. When you log in, it's often with greater permissions than the webserver has. Can you tell us who owns the file (user & group) and what user & group your web server is? – JBH Aug 14 '17 at 18:14
  • Does `$filename` contains the full path to the file? Does the user has proper permission to change permission on this file? – Felippe Duarte Aug 14 '17 at 18:14
  • Not totally familiar with this functions but based on the comments on the php docs, you may need to put your mode number (0777) in the function `octdec()` instead of the quotes. – yanman1234 Aug 14 '17 at 18:17
  • You php running as apache or php user, to be able chmod inside folder, you have to change the owner of this folder or add change permissions for specific user. Example: setfacl -m u:php:rwx myfolder – Oleg Imanilov Aug 14 '17 at 18:17
  • Possible duplicate of [php chmod() not changing permissions](https://stackoverflow.com/questions/15169684/php-chmod-not-changing-permissions) – localheinz Aug 14 '17 at 20:11
  • Decimal integer `777` is equal to `1411` octal. – Salman A Jul 05 '18 at 09:00

2 Answers2

6

The problem has to do with data conversion.

$chmod = "0777";
chmod($filename, octdec($chmod));

By just passing in the $chmod string it get converted 777 witch is not giving you want. octdec("0777") will output 511 that decimal will give chmod the value you want.

Jason K
  • 1,406
  • 1
  • 12
  • 15
0

Check the file path and file name is correct! then try this

chmod("/somedir/somefile", 0755);
Aniket Singh
  • 857
  • 4
  • 16
  • 39