Explorar o código

Change if/else to try catch

samuelayo %!s(int64=6) %!d(string=hai) anos
pai
achega
39af08c076
Modificáronse 1 ficheiros con 4 adicións e 3 borrados
  1. 4 3
      examples/php-xhr/upload.php

+ 4 - 3
examples/php-xhr/upload.php

@@ -21,16 +21,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
 if ($_POST && !empty($_FILES["files"])) {
     $target_dir = './uploads/';
     $target_file = $target_dir . basename($_FILES['files']['name'][0]);
-    if (move_uploaded_file($_FILES['files']['tmp_name'][0], $target_file)) {
+    try {
+        move_uploaded_file($_FILES['files']['tmp_name'][0], $target_file);
         header('Access-Control-Allow-Origin: *');
         header('Content-type: application/json');
         $data = ['url' => $target_file, 'message' => 'The file ' . basename($_FILES['files']['name'][0]) . ' has been uploaded.'];
         http_response_code(201);
         echo json_encode($data);
-    } else {
+    } catch (\Throwable $th) {
         header('Access-Control-Allow-Origin: *');
         header('Content-type: application/json');
-        $data = ['message' => 'Sorry, there was an error uploading your file.'];
+        $data = ['message' => 'Sorry, there was an error uploading your file.', 'error'=>$th->getMessage()];
         http_response_code(400);
         echo json_encode($data);
     }