Having Trouble Writing to a File with PHP on Ubuntu

Having trouble writing to a file with PHP on Ubuntu

After some more research, I've got it figured out. Here is the solution for anyone having the same problem:


Since localhost belongs to the www-data group, I just added my user to that group.

sudo usermod -a -G www-data my_username

Then, I added the folder to the group.

sudo chgrp -R www-data /var/www

Then, I gave write permissions to the www-data group.

sudo chmod -R g+w /var/www

This worked for me without any other issue. Thanks!

Why is this PHP code not opening or writing to a file?

Check your directory
add dot-slash (./) at the beginning of your directory

$fh = fopen("./test.txt",'a'); //open file and create if does not exist

unable to open file with php on ubuntu machine

In your script use

fopen(dirname(__FILE__) . '/inc/users/future.php', 'w')

This will create a filepath from the directory your index.php. If you script is called from another file, php might search coming from that file.

Also check if the php process has sufficient file permissions to read and open the file. Try setting the file to chmod 777 just to test if that is the case (do not leave it on 777 though).

Keep in mind that if the file is a symbolic link, the 'w' parameter of fopen will not work.

In PHP, why won't this write to test.txt?

Write this in a console

chmod a+w test.txt 

How to write a file with PHP in Linux?

fopen and fwrite work exactly the same on Linux, but you may have a permission issue there. To solve this, you need to check:

  • the user under which PHP runs (in a vanilla Apache/PHP install, this is usually www-data)
  • the permissions and ownership of the directory you are trying to write to

What you need is write permission on the directory for the PHP/Apache user. The easiest (and dirtiest) way of achieving this is is to just make the directory world-writable (chmod o+w the_dir), but this is highly insecure, allowing anyone with any kind of access to the system to store stuff there. A better solution is to either make the PHP user the owner of the directory and specify permission 700 (or 755 if you want it to be world-readable), or create a new group, put the PHP user and yourself in that group, set the group on the directory, and set permission 770 (775 for world-readability) on the directory.

Ubuntu allow www-data to write to new files

You will need to make sure that the directory you are writing to allows for www-data to write to it. Typically, you will want to put that directory in a place that is away from other files etc...

sudo chown www-data:www-data <DIRNAME>
sudo chmod g+w <DIRNAME>

should do the trick for you.

If the filename you are writing to already exists, the same commands above, applied to the file itself, should work as well. Typically, if the PHP script you wrote is creating the file, and failing, it's due to the parent directory permissions. For clarity - the commands that would adjust the file permissions and ownership.

sudo chown www-data:www-data <FILENAME>
sudo chmod g+w <FILENAME>

Finally, should you not be able to adjust the ownership of either the directory or the file, you can assign other or all access. But I would highly recommend NOT doing this for various scary security reasons.

Unable to create a file on ubuntu using php

You're trying to read a file because of that flag you're using r (read).

The flag r stands for:

'r' Open for reading only; place the file pointer at the beginning of the file.

You can use a+ read/write. If the file does not exist, attempt to create it.

$handle = fopen('demo.txt','a+') or die('unable to create');

If you want more clarity. Kindly visit the manual for more details. (Check out the modes part).

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.



Related Topics



Leave a reply



Submit