ungalnanban
ungalnanban

Reputation: 10327

fopen & fwrite function is not working from browser?

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.

Upvotes: 1

Views: 3114

Answers (5)

Bug Magnet
Bug Magnet

Reputation: 2668

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.

Upvotes: 5

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76541

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+");

Upvotes: 1

johnny_bgoode
johnny_bgoode

Reputation: 357

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

Upvotes: 2

Your Common Sense
Your Common Sense

Reputation: 157896

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

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798686

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.

Upvotes: 1

Related Questions