Search code examples
phpapachecentoscodeigniter-3chmod

PHP - chmod(): Operation not permitted


I have files that owned by root and I want to change it permission with php using chmod(). But it gave me an error chmod(): Operation not permitted instead.

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

PHP Error Response

2

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

Application Environment:

  • PHP 7.1.33
  • Code Igniter 3 framework
  • Apache 2.4.6
  • CentOS Linux release 7.8.2003

Any answer are appreciated, thanks before.

EDIT:

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

1


Solution

  • As the files are owned by root and not www-data, apache will not have permissions to change the file's read and write permissions. You would need to set the folder to be owned and writable by www-data.

    The command in centOS for doing so is

    sudo chown -R apache:apache ./filepath 
    

    You would need sudo for the root owner as well as replace filepath with your folder's name