test.php opens test.txt and writes to it. It works fine on one hosting server.
After migrating the code to another host, test.php now can't write out test.txt. The error shows:
Warning: fopen(/home/username/public_html/test.txt)
[function.fopen]: failed to open stream: Permission denied
It will only work when the test.txt file is set to 777. On the original host, test.txt was set to 755.
What am I missing to give permission for test.php to write to the file without 777 on this new host?
Also, file upload PHP scripts aren't working either.
It is not just the permission that is important, but also who owns the files. 755 is enough if the folder/file is owned by the same user that is used for running the web server.
Writing directly to the public_html directory is a huge security risk. I suggest creating a subdirectory (probably best outside the public_html) folder and give it the appropriate permissions / ownership.
It is safer to have the folder owned by the user that runs apache (or whichever web server is being used) - usually something like apache or www-data instead of giving world write permissions.
Related
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.
I am using Ubuntu as my OS, and I have XAMPP in it. In the htdocs folder, I have created a project folder in which I have index.php file. Now inside the index.php, a part of the code does this:
fopen("colors_new.csv", "w");
From the manual,
'w' Open for writing only; place the file pointer at the beginning of
the file and truncate the file to zero length. If the file does not
exist, attempt to create it.
But instead of the file being created, I am getting
Warning: fopen(colors_new.csv): failed to open stream: Permission denied...
And when I look into the project folder, the colors_new.csv file is NOT created.
When I run this program in another computer running Windows OS, it runs successfully. The file is successfully created.
How do I fix this problem in Ubuntu?
WHAT I TRIED:
I tried to run sudo chmod 777 colors_new.csv but I get chmod: can not access colors_new.csv. No such file or directory.. I tried this with just the file name (as shown) while the terminal WAS in the project directory; as well as by giving the full absolute path of the file. Got the same error both times.
NOTE: I have seen other questions, and as indicated above, I tried to give permissions to the file. The other questions did not solve the problem, the accepted answer below did solve it.
This means that you don't have the rights to write a file in the directory where your index.php is. A couple of things you can do to make it work
give filewriting permissions to the directory.
Go with 755 on that and 644 on files, if settings are correct this is sufficient. Never go full 777, which is security risk.
create a subdirectory inside your directory and give that sufficient rights (755) to do what you want.
F.i. Fopen('./files/colors_new.csv)
Provide the write permission on the folder in which you want to write the file. Your problem will be solved.
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.
For a project I need my php file to create a dynamic file (on remote server which I bought) which loads into a flash component. Everything is working fine on my localhost. But once I upload it to the server, it throws the following errors:
Warning: fopen(output.html) [function.fopen]: failed to open stream: Permission denied in C:\Inetpub\vhosts\x.co.in\httpdocs\blabla.php on line 25
Warning: DOMDocument::save(k_id.xml) [domdocument.save]: failed to open stream: Permission denied in C:\Inetpub\vhosts\x.co.in\httpdocs\blabla.php on line 136
where on those lines fopen is written.
I understood as there is no permission to my php file to create any dynamic files on the server. So i just wanna understand is there a way by which I can privilege my php to create that file on the server.
I've the login access, which I think I've to put somewhere in code before it tries to create such file...but i dont know how to figure this...any suggestions?
The user that runs the PHP process needs to have permissions to write new files in the target folder. On linux servers this is done using CHMOD.
chmod 777 -R /path/to/folder
777 is the permission (full permission for testing only), -R means recursive for the files/folders inside. As you are using windows just right click on the folder and look at properties and search for permissions.
Create a new directory where the PHP will write the files. For that directory set permissions to 705 (that is owner has Read, Write, Execute permission)
You can either do that using a FTP client (Filezilla) or via a Bash shell
mkdir inputfiles
chmod 705 inputfiles