PHP - chmod(): Permission denied
P粉475126941
P粉475126941 2024-03-25 22:25:57
0
1
451

I have a file owned by root and I want to change its permissions using chmod(). But it gave me an error chmod(): Operation not allowed instead.

if (file_exists($filepath)) {
    chmod($filepath, 0755);
}

PHP Error Response

How to use chmod() in php but the file ownership is root? Can I achieve this without changing the file ownership?

Application Environment:

  • PHP 7.1.33
  • Code Igniter 3 Framework
  • Apache2.4.6
  • CentOS Linux Version 7.8.2003

Any answers are appreciated, thank you.

edit:

I've run ps aux | grep httpd and it only shows root and apache in the list.

P粉475126941
P粉475126941

reply all(1)
P粉338969567

Since these files belong to root and not www-data, apache will not have the permission to change the read and write permissions of this file. You need to make this folder owned by www-data and writable.

The command to do this in centOS is

sudo chown -R apache:apache ./filepath

You need to sudo as the root owner and replace the file path with your folder name

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template