Jump to content

Problem with creation of files in linux.


Go to solution Solved by Ch0cu3r,

Recommended Posts

Hey, guys, in the following code, why i cannot see my ~/test.txt file? It seem he is not doing what i want, to create a .txt file with 'is just a test'. I'm using Debian.

 

 

<?php
$f = fopen("~/test.txt","w+");
fwrite($f,'is just a test');
fclose($f);
?>

 

Thank you!

Link to comment
https://forums.phpfreaks.com/topic/290611-problem-with-creation-of-files-in-linux/
Share on other sites

@claudiogc the ~ char will refer to the active users home directory. This will return your home directory when used in a terminal, as you are the user executing the command.

 

But when the php code is process by PHP it'll be ran as a different user. So the ~ will refer to the user PHP is being running as. Which I believe with Debian it'll be the httpd user.

 

So in order for the text file be created in your home directory you'll need to specify the full path to your home directory.

But i did what you said(specify the full path to your home directory) and still not working!

 

 

<?php
$f = fopen("/home/rob/test.txt","w+");
fwrite($f,'is just a test');
fclose($f);
?>

 

The same thing with the following code.

<?php
$f = fopen("test.txt","w+");
fwrite($f,'is just a test');
fclose($f);
?>

 

Maybe he is creating this file in other place than /var/www/ or /home/rob/

The webuser (user server is running as, not your account user) must have user or group file permissions to access/read/write on a dir outside of your webroot. Imagine the security implications if anyone on the server could just read/write any file they wanted.

The webuser (user server is running as, not your account user) must have user or group file permissions to access/read/write on a dir outside of your webroot. Imagine the security implications if anyone on the server could just read/write any file they wanted.

Who is the webuser in my case? Me? Apacha2? How can i chage these permissions to access/read/write on a dir outside of my weboot?

 

 

But i did what you said(specify the full path to your home directory) and still not working!

This is the expected behaviour if you have not set the necessary file permissions to allow PHP to create the file.

 

You need to read up on file permissions to understand how permissions work under Debian (the rules are the same for any *nix os). By default only you are able to create/modify files and folders in you home directory as they are owned by you. Any other user (other than the root user) will be prohibited from creating/modifying any files/folders within your home directory.

 

On Debian PHP is ran as the www-data user (under the www-data group) when your code is being ran. This is why PHP cannot create the text file in your home folder. Because PHP is being ran as different user and group it is being denied access to your home directory so the file is not being created.

 

To allow PHP to create a files in your home directory you need to do two things. Set the www-data group as the group owner for your home directory and apply write permissions for the group owner. You can do this by running these two commands: (NOTE: Replace <your_username_here> with your Debian username)

sudo chown <your_username_here>:www-data /home/<your_username_here>/
sudo chmod w+g /home/<your_username_here>/

Now when you run your code, PHP should now be able to create the text file within your home directory.

 

But this should not be needed at all. Why are you requiring PHP to able to create files in your home folder and not your document root?

This is the expected behaviour if you have not set the necessary file permissions to allow PHP to create the file.

 

You need to read up on file permissions to understand how permissions work under Debian (the rules are the same for any *nix os). By default only you are able to create/modify files and folders in you home directory as they are owned by you. Any other user (other than the root user) will be prohibited from creating/modifying any files/folders within your home directory.

 

On Debian PHP is ran as the www-data user (under the www-data group) when your code is being ran. This is why PHP cannot create the text file in your home folder. Because PHP is being ran as different user and group it is being denied access to your home directory so the file is not being created.

 

To allow PHP to create a files in your home directory you need to do two things. Set the www-data group as the group owner for your home directory and apply write permissions for the group owner. You can do this by running these two commands: (NOTE: Replace <your_username_here> with your Debian username)

sudo chown <your_username_here>:www-data /home/<your_username_here>/
sudo chmod w+g /home/<your_username_here>/

Now when you run your code, PHP should now be able to create the text file within your home directory.

 

But this should not be needed at all. Why are you requiring PHP to able to create files in your home folder and not your document root?

 

But why even if i change the  path to /var/www/test.txt he doesn't create him? If i change this to just the file name i don't know where he is created because /var/www/ is "empty", only .php files are there.

  • Solution

 

 

But why even if i change the  path to /var/www/test.txt he doesn't create him?

Most likely due to the file permissions set for the www/ directory.

 

 

 

If i change this to just the file name i don't know where he is created because /var/www/ is "empty", only .php files are there.

PHP has not created the file any where else. It will only attempt to create the file in the path you have specified

 

You need to do as Jacques1 mentioned earlier and turn error reporting on. So PHP will tell you what is wrong in the event an error occurs. Add the following two lines at the top of your code before ( after the <?php )

ini_set('display_errors', true);
error_repoting(E_ALL);

When you run your code now you should see a bunch of errors displayed mentioning PHP does not have permission to create the file in /var/www

 

To resolve the error you need to set the necessary file permissions to allow PHP to write to the /var/www directory. You can do as we did earlier with your home directory. By setting the www-data group as the group owner to /var/www and allowing the group owner to write to the directory, eg

sudo chown root:www-data /var/www/
sudo chmod g+w /var/www/ 

Running code now will allow PHP to create the file.

 

You need to read the link I gave you to understand how file permissions work. 

Edited by Ch0cu3r

Thank you, guy!

I did what you, Ch0cu3r said and look what i get:

 

 

ls -l /var

...

drwxrwxr-x  2 root  www-data 4096 Aug 21 21:51 www

 

Whe i try to execute this code:

 

<?php
ini_set('display_errors', true);
error_repoting(E_ALL);
$f = fopen("/var/www/filetest.txt","w+");
fwrite($f,'is just a test');
fclose($f);
?>

 

 

 

Fatal error: Call to undefined function error_repoting() in /var/www/teste.php on line 3
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.