views:

403

answers:

6

I'm trying to use PHP to create a file, but it isn't working. I am assuming this is because it doesn't have write access (it's always been the problem before). I tried to test if this was the problem by making the folder chmod 0777, but that just ended up making every script in that directory return a 500 error message until I changed it back. How do I give PHP write access to my file system so it can a create a file?

Edit: It is hosted on Hostgator shared hosting using Apache.

Edit 2: Someone asked for the code: The code is a GD image script. I know the rest of it works as previously I was creating the image every ime it was called. Now I am trying to create them when new text is added and save them to a folder. The write line I have is: imagejpeg(null,$file,85);

I also created a test file to check if it was just a broken script (mainly copied from tizag): http://gearboxshow.info/rkr/lesig.jpg/testfile.txt (I don't know if/how to post the code here properly. Here is the contents of the PHP script, minus PHP tags.)

It returns 13,13,1 (separate lines), so it looks as if it thinks it wrote something, but the testfile.txt is blank (I uploaded a blank one), or non-existent (if I delete it).

Edit 3: The server runs CentOS.

A: 

Here is a very good tutorial that might give you an idea what permission to use.

Web Logic
So I should give write access to group?If so, any idea why I'm getting 500 internal server errors?
SGWebsNow
+2  A: 

An very easy way is to let PHP create the directory itself in the first place.

<?php
 $dir = 'myDir';

 // create new directory with 777 permissions if it does not exist yet
 // owner will be the user/group the PHP script is run under
 if ( !file_exists($dir) ) {
  mkdir ($dir, 0777);
 }

 file_put_contents ($dir.'/test.txt', 'Hello File');

This saves you the hassle with permissions.

favo
That sounds like a good idea. But wouldn't that final 7 make it a completely open directory, allowing anyone at all to mess with it?
SGWebsNow
Thats right. You can modify the permissions to meet your needs.
favo
+1  A: 

You can change the permissions of a folder with PHP's chmod(). More information on how to use the command is here: http://php.net/manual/en/function.chmod.php

If you get a 500 Error when setting the permissions to 777 (world writable), then it means your server is setup to prevent executing such files. This is done for security reasons. In that case, you will want to use 755 as the highest permissions on a file.

If there is an error_log file that is generated in the folder where you are executing the PHP document, you will want to view the last few entries. This will give you an idea where the script is failing.

For help with PHP file manipulation, I use http://www.tizag.com/phpT/filewrite.php as a resource.

Ryan Gyure
The folder is already currently set at 0755 as default.I never thought of checking the error_log, but I just did and it doesn't contain anything about why it was failing to produce the files.
SGWebsNow
It might be a good idea to post the code you are having trouble with. A nice resource I use on occasion for file handler syntax help is http://www.tizag.com/phpT/filewrite.php.
Ryan Gyure
Edited main post with code.
SGWebsNow
+1  A: 

Set the owner of the directory to the user running apache. Often nobody on linux

chown nobody:nobody <dirname>

This way your folder will not be world writable, but still writable for apache :)

Thomas Winsnes
A: 

OK, I just set up a new server and all my folders set to 0777 give the 500 server error. I have never had this problem before. I see it is a security feature but how do I remove it?

Michael Howey
A: 

I'm dealing with this same issue. http://superuser.com/questions/179693/drupal-install-and-permissions

I've tried

chmod -R apache:apache *

I used the following script to get the current user name. Though still no luck.

<?php
passthru("whoami");
?>
Richard