How to give PHP write permission to a directory?
P粉512363233
P粉512363233 2023-08-27 15:25:11
0
2
612
<p>I'm trying to create a file using PHP but it's not working. I'm assuming this is because it doesn't have write access (has been this problem before). I tried to test if this was the problem by chmod 0777 the folder, but this ended up causing every script in that directory to return a 500 error message until I changed it back. How do I give PHP write access to my file system so it can create files? </p> <p>EDIT: It is hosted on Hostgator shared hosting using Apache. </p> <p>Edit 2: Someone asked about the code: The code is a GD image script. I know the rest of it works just like before when I created the image every time I called it. Now I try to create them when adding new text and save them to a folder. My write line is: imagejpeg(null,$file,85);</p> <p>I also created a test file to check if it was just a broken script (mostly copied from tizag): http://gearboxshow.info/rkr/lesig.jpg/testfile.txt (I don't know if/how to post the code here correctly. This is the content of the PHP script, minus the PHP tags.)</p> <p> It returns 13,13,1 (separate lines), so it looks like it thinks it wrote something, but testfile.txt is blank (I uploaded a blank one), or doesn't exist (if I delete it)). </p> <p>Edit 3: The server is running CentOS. </p>
P粉512363233
P粉512363233

reply all(2)
P粉376738875

Simple three-step solution


Step 1: Identify PHP User

Create a PHP file with the following content:

<?php echo `whoami`; ?>

Upload it to your web server. The output should resemble the following:

www-data

Therefore, the PHP user is www-data.


Step 2: Determine the directory owner

Next, check the details of the web directory via the command line:

ls -dl /var/www/example.com/public_html/example-folder

The result should look similar to the following:

drwxrwxr-x 2 exampleuser1 exampleuser2 4096 Mar 29 16:34 example-folder

Therefore, the owner of this directory is exampleuser1.


Step 3: Change directory owner to PHP user

Then, change the owner of the web directory to the PHP user:

sudo chown -R www-data /var/www/example.com/public_html/example-folder

Verify that the owner of the web directory has changed:

ls -dl /var/www/example.com/public_html/example-folder

The result should look similar to the following:

drwxrwxr-x 2 www-data exampleuser2 4096 Mar 29 16:34 example-folder

At this point, the owner of example-folder has been successfully changed to the PHP user: www-data.


Finish! PHP should now be able to write to the directory.

P粉352408038

A simple way is to have PHP create the directory itself first.

<?php
 $dir = 'myDir';

 // create new directory with 744 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, 0744);
 }

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

This can save you trouble with permissions.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template