Jump to content

[SOLVED] file permissions (write to file)


Recommended Posts

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)

Link to comment
https://forums.phpfreaks.com/topic/37913-solved-file-permissions-write-to-file/
Share on other sites

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.

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

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.