I am using the PHP copy function in part of my program, where the parameters of the function depend on user input. I am trying to avoid the default php error message if they include a path that the function cannot use, and output a custom message like shown below. I am new to handling errors/ exceptions. I am still getting the php default error message instead of the custom 'Path was incorrect!' using the method below.
What I tried:
try{
copy($webImagePath, $destinationPath);
}
catch(Exception $e){
echo 'Path was incorrect!';
}
Consider set_error_handler
: http://php.net/manual/en/function.set-error-handler.php
Example:
set_error_handler("someFunction");
function someFunction($errno, $errstr) {
output details and information
}
You should have thrown an Exception with custom message you wished to display. You can either suppress the default PHP error message for individual statements by preceding them with an '@' symbol. Or you might change error_reporting level per page( check this link for more details:http://php.net/manual/en/function.error-reporting.php
Also check the folowing code snippet
$file='example.txt'; //replace the file name with your URL
$newfile = 'example.txt.bak';
try{
$ret=@copy($file, $newfile);
if (!$ret){
throw new Exception("File path doesn't exist..");
}
}
catch(Exception $e){
echo $e->getMessage();
}