How to Give PHP Write Access to a Directory

How do I give PHP write access to a directory?

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

<?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 saves you the hassle with permissions.

PHP and file write permissions

Ok, so with the help of everyone who responded, I have worked out a solution:
The email and txt scripts are run by user 'owner' and the htmlPOST is run by user '?'

I had to make the folder chmod 777 in order for user '?' to work

When each script runs, it checks for the file 'output.php'. If it didn't exist, then after the fclose I added a chmod 777 - that way the scripts run by other users could open/write it later. If the file already existed, then I didn't add the chmod because if it was the wrong user it created an error.
So a simple example of it:

$data = '//data fields condensed into a single string obtained from email or txt';
$filename= "output.php";

if (file_exists($filename)){
$newFile= fopen($filename, 'w+');
fwrite($newFile, $data);
fclose($newFile);
} else {
$newFile= fopen($filename, 'w+');
fwrite($newFile, $data);
fclose($newFile);

chmod($filename, 0777);
}

Thanks for your help!

php permissions to create a file

Writing into a folder requires the Apache user to have writing, reading and executing privileges on that folder.

  1. So, first try to identify the name of the Apache user (often www-data).

  2. Then check if that user is either the owner or in the group of the folder where you want to write files.

  3. Give write, read and execute (7) privileges on that folder for that user. Give everyone else who don't need writing the read and execute privileges (5) on the same folder.

  4. (recommended) Give write and read (6) privileges to your files for the www-data user. Everyone else only need read privileges (4).

If www-data is neither the owner nor in the group of the file, then you should change either one of them. After doing this, you may find yourself unable to access the web folder if you access the server with a user other than www-data and other than root (like "webeditor"), and that user is neither the owner nor in the group.

I recommend:

  1. Set the owner and group to the Apache user/group.

    chown -R www-data:www-data /var/www
  2. Add the webeditor user (or whichever you use to connect to the server on ssh or ftp) to the www-data group.

    usermod -a -G www-data webeditor
  3. Give folders the write, read and execute privileges to the owner. Avoid the writing privileges on everyone else.

    find /var/www -type d -exec chmod 755 {} \;
  4. Files do not require the execution privilege. Only reading and writing is necessary for the www-data user, the rest only need reading privileges, so 644 is enough for our files.

    find /var/www -type f -exec chmod 644 {} \;

php write file and set permission

Yes, you can thanks to PHP CHMOD

// Read and write for owner, read for everybody else
chmod("/somedir/somefile", 0644);

How to give apache permission to write to home directory?

As your file residing in your Home directory, I would suggest one of following approaches.

  1. Give 0777 permission to file itself.

    chmod 0777 /home/djameson/test.txt
  2. Change Ownership to apache user www-data and give owner-write permission.

    sudo chown www-data:www-data /home/djameson/test.txt
    chmod 0744 /home/djameson/test.txt
  3. Add your user to www-data group or vice-verse add www-data user to your group. And then group write permission.

    sudo usermod -a -G www-data djameson
    chmod 0764 /home/djameson/test.txt

NOTE : I am assuming apache user name & group name is www-data & www-data respectively. You must change accordingly your server apache username/group name.

Giving PHP write permission in Apache

You need to check if the user under which runs apache has permission to write into the directory.

So it's like this:

Your apache server is process. The process runs under some user (say www). The PHP runs under apache. So if you try to write into a directory in PHP it is the same as if the user www logs into the server and tries to create a file in the same directory. So check who is owner of that directory and which permission do it have. You can do it e.g. via ls -la command. If www will be owner of that directory, you will be 100% safe ...



Related Topics



Leave a reply



Submit