Home  >  Q&A  >  body text

PHP - chmod(): Permission denied

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:

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粉475126941P粉475126941231 days ago375

reply all(1)I'll reply

  • P粉338969567

    P粉3389695672024-03-26 21:53:40

    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

    reply
    0
  • Cancelreply