07-17-2008, 04:07 AM
I'm a first-time PHP user, and I'm trying to use the fopen() function in order to create a file so that I can write to it. I believe I've used the function correctly, but for some reason, it keeps telling me that it can't open the file. I can't figure the problem out. Can anyone help?
My code is below:
$ourFileName = "testFile.txt";
$fh = fopen($ourFileName, "a") or die("Can't open $ourFileName");
When I try to load the webpage, I get the error message "Can't open testFile.txt". So clearly it's dying. What am I doing wrong? I should mention that I'm using a Linux server with PHP version 5.2.6. Any help would be much appreciated.
07-17-2008, 04:21 AM
Get php to tell you why the function failed by turning on full php error reporting. Add the following two lines after your first opening <?php tag -
ini_set ("display_errors", "1");
07-17-2008, 04:22 AM
File permissions may be disabling you're ability to create new files. Enable you're error reporting: Error_reporting(E_ALL) and possibly ini_set('display_errors', 1); at the top of you're script. It will issue an actual warning telling you what the problem is.
If safe mode is enabled you'll need to chown the directory to apache so it has the ability to write files to it.
07-17-2008, 04:22 AM
I my self am new with php but I have been writing c for a long time and using Linux for years. So I hope this helps!
This may seem a bit trivial but what are the current privileges on that file? If you do a
( ls -l ) or ( ll ) while in the same directory check that you have sufficient access. If your not concerned security on that file do ( chmod 777 testFile.txt) This might be your problem.
Good luck I will check back to see if that did it for you.
07-17-2008, 12:11 PM
Thank you all very much! You were all right, turning on full php error reporting revealed that I did not have sufficient file privileges. I fixed that by creating the file beforehand and doing a chmod 777 on it, and I was then able to open and write to the file. But that was a previously created file. My question now is how to create a file from scratch using php without giving everyone rw access to my directory. I guess this is the chown part.. how do I do that?
07-17-2008, 06:27 PM
I would use a new directory and get PHP to create it with the mkdir command. Set it with the mode of 0660 for its permissions, and I believe that mkdir defaults the chown to Apache or whatever the name is for the user running under apache. Note with 660 permissions, you will lose the ability to navigate this directory on you're own. I don't have my linux box working yet so I can't actually test this out for you right now, sorry.
Chown information can be found here (http://ca3.php.net/manual/en/function.chown.php). Another option is to use ssh or ftp to create a new directory and apply the permissions 0666 (r/w for user, group and everyone) since PHP will fall into everyone.
So long as you do it on the containing directory, PHP will attempt to make the file and if they own the directory and have a bit 4 permissions they will succeed.
And finally, I don't recommend the use of 7 for any of you're permissions on web environments. 7 includes the execute privilege (1) and if you allow users to upload files of their own, creating a sh/perl/php/etc file would be a piece of cake to do. Now you have a file that can be directly executed by any use who attaches to the file. Bad.