claudiogc Posted August 23, 2014 Share Posted August 23, 2014 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! Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted August 23, 2014 Share Posted August 23, 2014 That should work fine, although you just need this. <?php $f = fopen("test.txt","w+"); fwrite($f,'is just a test'); fclose($f); ?> Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted August 23, 2014 Share Posted August 23, 2014 (edited) You cannot use “~” in fopen(). Edited August 23, 2014 by Jacques1 Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted August 23, 2014 Share Posted August 23, 2014 @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. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted August 23, 2014 Share Posted August 23, 2014 You cannot use “~” in fopen() at all. It's not evaluated. Quote Link to comment Share on other sites More sharing options...
claudiogc Posted August 24, 2014 Author Share Posted August 24, 2014 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/ Quote Link to comment Share on other sites More sharing options...
CroNiX Posted August 24, 2014 Share Posted August 24, 2014 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. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted August 24, 2014 Share Posted August 24, 2014 PHP actually tells you the exact reason for the error. Activate error reporting (if you haven't already), activate logging, run the script and then check your error log. Quote Link to comment Share on other sites More sharing options...
claudiogc Posted August 24, 2014 Author Share Posted August 24, 2014 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? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted August 24, 2014 Share Posted August 24, 2014 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? Quote Link to comment Share on other sites More sharing options...
claudiogc Posted August 24, 2014 Author Share Posted August 24, 2014 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. Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted August 24, 2014 Solution Share Posted August 24, 2014 (edited) 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 August 24, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
claudiogc Posted August 27, 2014 Author Share Posted August 27, 2014 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 Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted August 27, 2014 Share Posted August 27, 2014 (edited) Sorry misspelt the function, it should be error_reporting (left out the r between o and t ) on line 3. Edited August 27, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
claudiogc Posted August 28, 2014 Author Share Posted August 28, 2014 Sorry misspelt the function, it should be error_reporting (left out the r between o and t ) on line 3. Wow! LOL. I should have seen that too. Thank you very much for your help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.