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
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.
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
Related
I am running an apache webserver on a virtual machine on my pc. The vm is running zorin linux. I have written a php script that writes some data to another text file in the same directory. Both files are located in /var/www/html/. I have already verified that the webserver user is www-data, and I have attempted to make that user owner of the /var/www/html directory, as well as everything within that directory. Here is the command that I have used to do this:
sudoo chown -R www-data /var/www/html/*
I have also verified that the www-data user is the owner of this directory and it's contents using
ls -dl /var/www/html/
ls -dl /var/www/html/*
both return that the owner is www-data.
Even after doing all of this, the php file is still unable to write to the file correctly. At this point I am stumped. Any help would be greatly appreciated.
I have setup an linux CentOS 7 server to run php and i using apache, but when all done php can't write file even i have set chmod 777 and i have chown apache at the owner of the directory.
If the file exist, i can write the data on it, but if file don't exist
i can create it, it notice me don't have permission (folder is chmod
777).
Thanks for your help.
I get this error while trying to do file_put_contents().
Apache is working as apache group, started with sudo rights. My user is in apache group. All dirs and files have 755 chmod.
File exists.
Locally it works fine, on remote CentOS server not. Why? How to debug that thing?
Sometimes SELINUX will prevent writing as was my problem serving from Fedora. Run:
sudo setenforce 0
This can be solved by changing directory permission.
Run the command like
chmod 777 database (the directory)
You can use getcwd()" to find the directory path.
Please have a look here
I have a script that will generate an image then upload it in a certain directory. The script works fine, however uploading it in the CentOS server, it won't work anymore. By debugging, I have found out that this is a permission denied issue. I am forced to chmod -R 777 certain_directory to make it work.
I am not convince with this, I can't set the directory ownership to www-data I'm used to when I am using Debian. Any workaround for this?
# egrep ^User /etc/httpd/conf/httpd.conf
User apache
So just chown the files to apache...
I downloaded CakePHP and put it in my www directory. I enabled mod-rewrite and all that and changed my document root. But I still get permission denied errors in my apache error log because Cake can't require the files from other directories. It's almost as if I need to chmod every single directory to 777 in order to make this work. I know that's not right but can someone tell me if I did not install it correctly or what to CHMOD. Because so far it's literally every folder I'm going through CHMODing right now.
Try
chown -R <webserver_user> <cake_dir> #set the owner of your cake dir recursively to your webserver
chmod -R 755 <cake_dir> #set the permissions recursively
you can get the webserver user with
ps -aux | grep apache #or whatever your web server is