Reputation: 7053
I am trying to create a file and write a message in a folder called debug:
function debug($report){
$file="/debug/debug.txt";
file_put_contents($file,$report,FILE_APPEND | LOCK_EX);
}
The debug folder is in my root directory. But file fails to be created.. Why?
Upvotes: 0
Views: 141
Reputation: 10878
This is really a codicil to Lix's answer.
The most frequent reason for this is when you are running you scripts under Apache using mpd_php5, since your php process will be running as apache UID (www-data
or whatever). However, by default your debug directory will be own by your account with 755 permissions.
I would suggest that it is bad practice to set the permissions to 777 as this will allow anyone to create files in your directory, but instead temporarily set the permissions to 777 then use a Q&D script to create a debug/logs
and set its permissions to 755, before returning the debug directory to 755.
You will find that the debug/logs
directory is now own by the Apache UID, and can be written to by any Apache process.
One wrinkle here is that you will need to use a script to delete old debug files as your own account won't have the privilege to do this.
Upvotes: 1
Reputation: 1712
Nothing wrong with that code, if errors not shown maybe caused be php configuration. Please see at Error Reporting Error Report.
Generally it may caused by permission ( see permission on debug folder )
Upvotes: -1
Reputation: 47956
This code will open all the permissions for the folder.
chmod('PATH_TO_FOLDER',0777);
Depending on what exactly you want to do with that folder, you might consider giving it slightly more limited permissions that allow only specific users to read/write to it.
Here are some other examples taken from the PHP manual - http://php.net/manual/en/function.chmod.php
<?php
// Read and write for owner, nothing for everybody else
chmod("/somedir/somefile", 0600);
// Read and write for owner, read for everybody else
chmod("/somedir/somefile", 0644);
// Everything for owner, read and execute for others
chmod("/somedir/somefile", 0755);
// Everything for owner, read and execute for owner's group
chmod("/somedir/somefile", 0750);
?>
Upvotes: 2