2

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!';
 }
1
  • You could create your own error handler with set_error_handler and use functions like strpos, str_replace to change around your errors.. It's a little work, but it goes along way Commented Dec 21, 2012 at 2:53

2 Answers 2

3

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
}
Sign up to request clarification or add additional context in comments.

Comments

-1

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.