I am trying to run some PHP applications on CentOS powered server with apache and MySQL. My apps have to create files on server, but it always says that permission is denied to create a file.
Files are located in /var/www/html. I even tried setting 777 permission to html folder and html/*. I changed apache user and group to myuser, that exists, and restarted apache. I changed the ownership of html folder and all files inside to myuser. I even tried changing document root to /home/myuser/public_html
I tried this code to test write permission. File location is /var/www/html/index.php and /home/myuser/public_html/index.php
$handle = fopen("a.txt", "w");
fwrite($handle, "test");
fclose($handle);
I am just more than amazed by this problem. The same configuration works on my another Ubuntu server.
Some geniuses must be here, help me.
You need to allow you server user to do write operation on your directory, User the below command, If it is a multilevel directory use -R flag.
sudo chown www-data my-dir
Instead of manually giving permission you can try chmod($handle, 0777) within your code.
Related
I have a program that is being hosted on a linux box running Centos 7. The program is accepting form data and saving to a windows file server. the windows file server is mounted on the linux box. i have created a symlink in the directory that the program is in. everytime that i launch the program in the web browser and try and add the data, i receive the following error:
file_put_contents(/W/Shortage/2017/Apr/Shortage04-04-17.csv): failed to open stream: Permission denied in /var/www/html/DriverTest/driverss.php on line 78
In my smb.conf file, i have the property already set to follow symlinks. everyone has rwx rights to the files and the symlinks as well. this is the php code where the error is referring to:
if(!is_dir('/W/Shortage/'.$year. '/' .$month))
{
mkdir('/W/Shortage/'.$year. '/' .$month, 0777, true);
}
file_put_contents('/W/Shortage/'.$year. '/' .$month. '/Shortage'.date('m-d-y').".csv", $result, FILE_APPEND);
when i SSH into the system using a user that i created, i can cd into the directory and do touch test.txt and the file is created. i can also use vi to edit the test.txt file and enter data into it. in the directory, the owner of the files and folders are apache. i am not sure why i do not have any permissions to write to that directory. any suggestions or anyone seeing anything wrong with what i am doing so far?
I installed LAMP in my CENTOS and it all looks fine. In my www/html/ folder I placed my script and they all work fine, they can write/read everything. However everytime I create a folder and I put files inside it they cannot write, for example, if I use file_put_contents with PHP it does not work due the permission denied error.
Everytime it happens I need to chmod 777 to that folder and all the files inside. It's a great time loss.
Is there someway I configure the CENTOS so everytime I create a folder or upload a file it automatically will have read/write permission?
If you want new created folder/file to automatically have 777 permission, you must set umask to 000.
Ref: Setting the umask of the Apache user
The php script is unable to create file owing to denial of permissions.
PHP Script:
<?
echo exec('whoami');
file_put_contents('/var/www/html/sample.txt','Some random Text');
?>
Output :
apache
**Warning**: file_put_contents(/var/www/html/test/samds.txt): failed to open stream: Permission denied in **/var/www/html/index.php** on line **3**
The directory permissions are all 777
I have had same issue and i misplaced some configuration.Firstly remeber that the user who created the php file can read the file and if the user who created php file is different than the apache user in your case it is apache in that case you will get this exception if you try to write from different users.So what you have to do is simply,and i would suggest you to revoke the 777 permissions as i do not like idea of your files being publicity available on read and write to the world,so get it back to 755 and do the next on the command line of your server.
cd /var/www
change the user of the HTML only as we need it to be writable only,so do not touch the var and www,so basically as soon as you have navigated to he www directory you are going to change the HTML directory owner to the one who is currently running the apache server,and you got that info already,in your case it is apache so we are going to do the next,
chown apache:apache html
At this stage the html file owner will be apache so you will be able to write onto it.Anyway and meanwhile i would strongly suggest you to create separate directory inside html lets say testDir and try there,i would not suggest to change the root folder permissions,i had bad experience with them.Anyway i hope it will help as i suffer more then hour and just now got the right way to do it.This got my issue fixed,hope it will fix your's too.!
I have installed fedora15 on my system, installed apache and php5.3 and mysql. I have not changed any default setting in any configuration files
Created one ftpuser and uploaded files to Documentroot which is '/var/www/html/' and when I run php files all the files runs successfully.
But when it comes to file-uploads, fopen, fwrite, imagemagick convert etc all the programs which involve creation of files programatically does not work. I have given 777 permissions to the folder where I run the php scripts but still it gives me the access denied error.
Below is a sample code which creates permission denied error.
$handle = fopen("test.txt","w+");
if(!$handle)die("Could not open file for writing");
fwrite($handle,"Testing Uploads Successfull.");
fclose($handle);
The above file is located in '/var/www/html/test/' directory of my newly installed fedora15 system.
Though if I run this file as a root user using php command line it executes correctly creating a new file and the text inside it. If I run the same file in command line with another user it produces permission denied error, though the directory has got 777 permission over it.
Please any one help me with this wierd behaiour,
Thanks in advance.
SELinux is preventing the web server from creating and writing to files. See the httpd_selinux(8) man page for more details.
I recently moved my website to a new host and now am experiencing some broken code..
I have an uploading script that is now returning this:
move_uploaded_file() failed to open
stream: Permission denied in *..
I've set the upload directory to 777 which worked fine, but my script is needed to have top level permissions..
(As the script itself sets permission to directories, does lots of copying etc)
Is there a way in apache I can set the PHP script to the owner of all the folders on my server?
Thanks
Also
When looking in phpInfo()
Under
apache2handler
User/Group nobody(99)/99
Is this related?
I wouldn't go that route, just give it permissions to the defined upload_tmp_dir, or define upload_tmp_dir to be a directory you have access to. If it is that directory you have problems with. If the target is the problem, and you've 777'ed it, something fishy is going on.
Do you have ssh access to your new host? The reason I ask is that it's probably not best to use the username/group as nobody, as most other services would use this too. I would change it to something like apache
You can then update httpd.conf, adding in these two lines (reloading the config after):
User apache
Group apache
Then, run chown apache:apache -R dir_name to make apache own it.
well,
When you are trying to set the permission like "0777", you must be running on same authority.
What I mean is.
For example, your script tells to change a folder/file permission to 0777, but the folder or file already has a permission and that is '0755' so you are not authorised to make that change. as the user have only 5 authority.
Either, you need to login to FTP and change the folder permission to 0777 and then you have full control over it or you have to stick with using 0755 or similar.