tags:

views:

68

answers:

5

fopen function is not working in php.

I wrote the following code in my php file.

            $fh =  fopen("/home/sugumar/Public_html/sugumar/public_html/123","a+");

            fwrite($fh,"hello");

I ran this code from command line: php file_name.php its working fine. But If I run this code from browser it shows the following error.

Warning: fopen(Logs/add_employee.logs) [function.fopen]: failed to open stream: Permission denied in /home/sugumar/Public_html/sugumar/public_html/HRMS/HRMS_add_emp_DB.php on line 111

Warning: fwrite(): supplied argument is not a valid stream resource in /home/sugumar/Public_html/sugumar/public_html/HRMS/HRMS_add_emp_DB.php on line 113

how to solve this problem?

Thanks in advance.

+1  A: 

The file does not have the appropriate permissions set for the user the web server is running as to be able to modify it. Use chmod et alia to modify the permissions appropriately.

Ignacio Vazquez-Abrams
A: 

The error message is self-explanatory: Permission denied

You have to chmod your /Public_html/ to 0777 using FTP client, to give web-server write access to files in this directory

though uppercase P seems very suspicious

Col. Shrapnel
+1  A: 

You have to make the directory you are writing to writable by your webserver.

For example:

chown -R www-data:www-data /home/sugumar/Public_html/sugumar/public_html/123
chmod 644 /home/sugumar/Public_html/sugumar/public_html/123
johnny_bgoode
Yeah and lose FTP access to them
Col. Shrapnel
True, if you are using FTP it is better to modify the permissions of the directory only: chmod 777 /home/sugumar/Public_html/sugumar/public_html/123
johnny_bgoode
+1  A: 

As has been pointed out by the other users, when being run from the web server, you don't have permission to that file.

But rather then give permission to that directory, I would recommend using a different file:

$fh =  fopen("/var/tmp/123","a+");
R Samuel Klatchko
+3  A: 

I don't think it's a good idea at all to chmod your public HTML folder to 777. Besides the error message states that it is not the public_html folder that has permission problems, but the Logs folder... specifically this particular file in the Logs folder does not have "write" access: add_employee.logs

To change permissions, just find the location of the Logs folder, then if you have access to SSH, cd to the Logs folder on the server and enter:

chmod 666 add_employee.logs

This will change the permissions of the add_employee.logs to read + write, so your PHP will be able to write to the file.

If you don't have SSH, try using your FTP client to connect to the server. Then navigate to the Logs folder on your server (depends on whether your hosting provider allows FTP access to this folder). Depending on the FTP client you have, you should be able to right-click on a file/folder and change permissions for the selected file/folder.

If you can't access the Logs folder with FTP, your hosting provider should offer a CPanel website for you to login to. From the CPanel, you should have access to some kind of file manager, where you can access the Logs folder and can change the file's permissions from there. Can't give you instructions here as I have no idea which CPanel software your hosting provider uses and they're all very different.

Bug Magnet
Why set a logfile's owner-executable bit? That's just asking for trouble, especially when it's world-writable. Set to 666, unless you're superstitious and can't bear the number.
Piskvor
Good catch. I've revised the post to use The Number of The Beast now. Thanks!
Bug Magnet