Member Avatar for Zagga

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

You could opt for try ... catch.

Member Avatar for Zagga

Thanks twiss, that is exactly the sort of thing I am looking for.
It seems my adventures into oop are about to begin :)

Member Avatar for Zagga

After reading up some more about TRY and CATCH I realised that either this wasn't exactly what I needed, or, more likely, I didn't understand it enough to relate it to my problem properly.

Either way, it did lead me on to finding a solution, so I thought I would include it here for reference.

It turns out that all I actually needed to do was find a way to NOT display the E_WARNING message.
I could turn error reporting off, but I didn't like that idea.
I found out I could create a custom error handling routine and use it for the duration of my function, then switch back to the standard internal error reporting again.

This is the error reporting function that I included inside my put_file function. It is only triggered by E_WARNING messages.

function my_warning_handler($errno, $errstr){ // $errno and $errstr are sent to the function by the error that triggered it.
   if ($errno == E_WARNING){ // If the error code is an E_WARNING ...
//  Do nothing (warning is ignored, not displayed).
      return true; // Return true to indicate we have handled the error.
   }
   else{ // If the error code is anything other than E_WARNING ...
      return false; // Return false to indicate we have not handled the error and that the internal error handler should deal with it.
   }
}

// Set to the user defined error handler (above).
set_error_handler("my_warning_handler");

After the put_file function had run (before it returned) I switched back to the internal error reporting.

// Revert back to the internal error handler.
restore_error_handler();

I hope this is clear enough to help anyone with a similar problem.
Zagga

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.