View Full Version : the code work well when exceuted from the terminal and not when working with server

khalid omer
03-05-2009, 08:23 PM
i am new to the php and i need the help as soon as possible.
i have the problem that all the commands dealing with the file show the following error
Warning: file_put_contents(/var/www/html/test.txt) [function.file-put-contents]: failed to open stream: Permission denied in /var/www/html/num4.php on line 2
and this happens when the code is executed using the browser and the apache server
but the file commands work well and the file is created when executed from the terminal using the command
php -f num4.php
the code is as follow
file_put_contents( '/var/www/html/test.txt', "this is a test");

i have checked the safe_mode and it is off. what else i can do.

03-05-2009, 08:35 PM
If the code you are trying to use is in the html directory already you will want to simply write to the file name (test.txt) not the name and fully qualified path.

03-05-2009, 08:44 PM
I believe the CLI version when running takes the current user, while the module through apache takes apache's user (ie: Apache, nobody, etc).
So, that being said, the problem is you're PHP doesn't have write privilege on the file in question. Group the file(s) to the apache user, change the file ownership, or allow write privilege for the files to the everybody group (not recommended).

khalid omer
03-05-2009, 09:06 PM
i am new to the php and linux so i didn't know what to do with the code that you have written.
any way i put the code just before the file_put_contents command and this resulted in the following error on the screen
Parse error: syntax error, unexpected T_STRING in /var/www/html/num4.php on line 2
and no file were created

the new code is as follow
struct User *upFou;
userInit(upFou, 'Fou-Lu', 1);
printf("%s has %s to %s\n", (*upFou).Name, !quitSmoking(upFou) ? "FAILED" : "SUCCEEDED", (*upFou).Smoker == 1 ? "FAIL" : "PASS");
file_put_contents( "/var/www/html/test.txt", "this is a test");
thank you very much for helping
sorry iam taking from your valuable time

khalid omer
03-05-2009, 09:10 PM
thanks dave. but changing the path just to "test" didn't work. i have also tried the following command in the terminal
chmod 777 /var/www/html
and it didn't work neither

khalid omer
03-06-2009, 08:39 AM
thanks to all who tried to help. thanks dave an fou-lu. the problem has been resolved. i figured out that i didn't gave you the right information. i tried the chmod on file on the /etc and the system prevent this. (i tried to save the file in a directory inside the etc directory) the chmod work well when you are dealing with directory that is not critical like the etc
and since i need to save my files in the /etc the troubleshoot browser gave me the solution (the star above just beside the clock in the bar)
you need to excute the following command to enable the apache server to access the folder
chcon -t httpd_sys_content_t /etc/your_folder
thanks again. and sorry for wasting your valuable time.