1

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

  • Are you running the php file via the command line "cli" or in the browser with apache/nginx? – Jordan Casey Nov 15 '22 at 04:12
  • 1
    Does this answer your question? [PHP chmod( ):Operation not permitted, safe\_mode deprecation involved?](https://stackoverflow.com/questions/23070266/php-chmod-operation-not-permitted-safe-mode-deprecation-involved) – chiliNUT Nov 15 '22 at 04:12
  • @JordanCasey Im using it in the browser with apache – Gabriel Ronaldo Nov 15 '22 at 04:17
  • @chiliNUT Thanks for the info, I've run ps aux | grep httpd and there is only root and apache on the list, what's that mean ? – Gabriel Ronaldo Nov 15 '22 at 04:25
  • The file is owned by root, so you can't change it by apache user. – shingo Nov 15 '22 at 04:47
  • @shingo thanks for the info. if that so, is there a way to change the ownership programmatically using PHP ? thanks before. – Gabriel Ronaldo Nov 15 '22 at 05:44
  • No unless PHP is running by root, but this is dangerous. https://askubuntu.com/questions/116144/how-do-i-run-apache-as-root – shingo Nov 15 '22 at 06:44

1 Answers1

-3

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

Jordan Casey
  • 955
  • 9
  • 16
  • I've run it but it says `chown: invalid user: 'www-data:www-data'` And does that mean I need to change the ownership of the file to www-data each time a new file stored to the same folder by root ? – Gabriel Ronaldo Nov 15 '22 at 04:31
  • i'm sorry, www-data is a debian user. You would need to use httpd:httpd and yes it does mean if you're storing files by hand that if you want chmod to work, they will need to belong to httpd – Jordan Casey Nov 15 '22 at 05:15
  • Thanks for the asnwer, I've run it again but still it says `chown: invalid user: 'httpd:httpd'`. Sorry, am I missing something ? – Gabriel Ronaldo Nov 15 '22 at 05:37
  • By the way, can I achieve that without changing the ownership or maybe can I change the ownership programmatically using PHP when the files belongs to root ? – Gabriel Ronaldo Nov 15 '22 at 05:40
  • you can run this to figure out who the apache user is ```ps aux | egrep '(apache|httpd)'``` then chown to that user. If you wanted to change the group ownership you could, It just leaves security a bit less strict so I do not do this. If the fire belongs to root, then root and SUDOers are the only ones that can change this. You could run a cron as root to change the permissions but again not secure. If someone injects a php or bash file to your location, it would be a bad time – Jordan Casey Nov 15 '22 at 06:36