Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

try catch statement in PHP where the file does not upload

I understand what try-catch statements do, but from reading the documentation on php.net, I would not be able to implement one into my own code. I need a real example to help me understand.

How can I turn this example into a try-catch statement, if the upload was not successful?

$move = move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/uploads/'.$_FILES['file']['name']);  if (!$move) {     die ('File didn\'t upload'); } else {                 //opens the uploaded file for extraction     echo 'Upload Complete!'; } 

This may not be a good example to work with, but any help would be appreciated.

like image 568
Ben McRae Avatar asked May 31 '09 23:05

Ben McRae


People also ask

How can I catch exception in PHP?

Because exceptions are objects, they all extend a built-in Exception class (see Throwing Exceptions in PHP), which means that catching every PHP exception thrown is as simple as type-hinting the global exception object, which is indicated by adding a backslash in front: try { // ... } catch ( \Exception $e ) { // ... }

Can we use try without catch in PHP?

You must use catch with try . Please look php.net manual. PHP has an exception model similar to that of other programming languages. An exception can be thrown, and caught ("catched") within PHP.

What is try catch finally in PHP?

Definition and Usage. The finally keyword is used in try... finally and try... catch... finally structures to run a block of code whether or not an exception occurred.

Can we use catch () without passing arguments in it?

Some exception can not be catch(Exception) catched. Below excecption in mono on linux, should catch without parameter. Otherwise runtime will ignore catch(Exception) statment.


2 Answers

You could do it like this.

try {     //throw exception if can't move the file     if (!move_uploaded_file( ... )) {         throw new Exception('Could not move file');     }      //do some more things with the file which may also throw an exception     //...      //ok if got here     echo "Upload Complete!"; } catch (Exception $e) {     die ('File did not upload: ' . $e->getMessage()); } 

It is a bit pointless for the above example, but you should get the idea. Note that you can throw the exception(s) from anywhere (e.g. within a function/method that you call from withing the try{}) and they will propagate upwards.

like image 105
Tom Haigh Avatar answered Sep 19 '22 23:09

Tom Haigh


Well, if you want to use exceptions, you could do something like:

function handleUpload() {       $move = move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT']."/uploads/".$_FILES['file']['name']);      if (!$move) {        throw new Exception('File Didnt Upload');     }  }  try {    handleUpload();    echo "File Uploaded Successfully"; } catch(Exception $ex) {    die($ex->getMessage); } 

I know this may seem like bloat - but you can call the method from anywhere in the call stack, and catch the exception at any point.

like image 27
Alistair Evans Avatar answered Sep 21 '22 23:09

Alistair Evans