Hi folks,
I am trying to create a function that adds comments to a text file.
If the file exists, the comments are added to it.
If the file doesn't exist, it is created then the comments are added to it.
If there are any problems, an error message is returned.
I am using the following function (messy and inefficient, but so is the mind that created it).
<?php
function put_file($file_path, $input){
$problem = "There was an error writing to the file.";
$function_error_message = "";
if (!file_exists($file_path) || is_writable($file_path)){
if (!file_put_contents($file_path, $input, FILE_APPEND)){
$function_error_message = $problem;
}
}
else{
$function_error_message = $problem;
}
return $function_error_message;
}
?>
There are 4 possible outcomes:
1) If the file exists and can be written to - all is good.
2) If the file doesn't exist but can be created - all is good.
3) If the file exists but I don't have permission to write to it (the root of a shared server for example) - unable to test as I don't know the server path to any existing files that are outside my account, so it usually defaults to possibility #4 below.
4) If the file doesn't exist and I don't have permission to create it - the function correctly returns my error message but I also get an E_Warning saying "[function.file_put_contents]: failed to open stream: Permission denied".
Is there any way to check if I have permission to create a file at a given location without using the file_put_contents
function?
(I am on a paid hosting plan on a shared server)
Zagga