jondo Posted February 10, 2007 Share Posted February 10, 2007 Hi, I am trying to write to a file with php <?php // set file to write $file = '/home/somedir/text.txt'; // check if file is writable if(is_writable($file)) { // open file $fh = fopen($file, 'w') or die('Could not open file!'); // write to file fwrite($fh, 'Hello, file!') or die('Could not write to file'); // close file fclose($fh); } else { print("file not writable"); } ?> I have set the file and directory it is in to 764 as I thought that would allow php to write to the file, however even after changing it to 764 the file is not writable. could someone please tell me the file permissions needed, or if 764 is correct, what am I doing wrong? any help is much appreciated. (by the way i realise if i just set it to 777 then it will probably work, but I remember reading that you don't want to give any unnecessary write access as this would allow anybody to delete the files or directories. all i want to do is add: the ability for php to write to a file, to the default permission, which is 755 as far as i remember) Quote Link to comment https://forums.phpfreaks.com/topic/37913-solved-file-permissions-write-to-file/ Share on other sites More sharing options...
trecool999 Posted February 10, 2007 Share Posted February 10, 2007 Try setting it to 755, worked for me . Quote Link to comment https://forums.phpfreaks.com/topic/37913-solved-file-permissions-write-to-file/#findComment-181464 Share on other sites More sharing options...
Hypnos Posted February 10, 2007 Share Posted February 10, 2007 755 should work if you chown the file to the same user as the user that apache/php is running under. If the system is otherwise secure (no blank password accounts, no public ftp access to this folder), 777 should be fine for this file. This file should be near your other public files. Not in a user's home directory. Quote Link to comment https://forums.phpfreaks.com/topic/37913-solved-file-permissions-write-to-file/#findComment-181477 Share on other sites More sharing options...
jondo Posted February 10, 2007 Author Share Posted February 10, 2007 when i used php to create a new file, i checked the permissions and it was 644, even less than i had before, and yet it works, i am just finishing for the day just now and can't be bothered experimenting any more today but tomorrow i will thoroughly check it and find out the exact threshold for writing, and will post when i do. thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/37913-solved-file-permissions-write-to-file/#findComment-181504 Share on other sites More sharing options...
trecool999 Posted February 10, 2007 Share Posted February 10, 2007 Well, have fun I guess . Quote Link to comment https://forums.phpfreaks.com/topic/37913-solved-file-permissions-write-to-file/#findComment-181506 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.