Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return error from php to dropzone.js

I would like to return an error on a case from my php code that handles the upload.

Currently if the php upload fails the JS still thinks it successes which I presume is to do with the fact it returned fine.

I have tried returning false instead of a string but that still runs the this.on('success') function.

PHP

public function imageUpload(){
    //Сheck that we have a file

    if((!empty($_FILES["file"])) && ($_FILES['file']['error'] == 0)) {

      //Check if the file is JPEG image and it's size is less than 350Kb
      $filename = basename($_FILES['file']['name']);
      $ext = substr($filename, strrpos($filename, '.') + 1);

      if (($ext == "jpg") && ($_FILES["file"]["type"] == "image/jpeg") && 
        ($_FILES["file"]["size"] < 3500000)) {

        //Determine the path to which we want to save this file
          $newname = '/home/anglicvw/public_html/newdev/app/templates/default/images/testimages/'.$filename;
          //Check if the file with the same name is already exists on the server

          if (!file_exists($newname)) {

            //Attempt to move the uploaded file to it's new place
            if ((move_uploaded_file($_FILES['file']['tmp_name'],$newname))) {
               echo "It's done! The file has been saved as: ".$newname;
            } else {
               echo "Error: A problem occurred during file upload!";
            }
          } else {
             echo "Error: File ".$_FILES["file"]["name"]." already exists";
          }
      } else {
         echo "Error: Only .jpg images under 350Kb are accepted for upload";
      }
    } else {
     echo "Error: No file uploaded";
    }
}

JS

$(document).ready(function(){

            Dropzone.autoDiscover = false;

            var myDropzone = new Dropzone('div#imagesdropzone', { url: '/admin/imageUpload',
            parallelUploads: 100,
            maxFiles: 100,});
        });

        Dropzone.options.imagesdropzone = {
            init: function() {
                this.on('success', function( file, resp ){
                  console.log( file );
                  console.log( resp );
                });
                this.on('error', function( e ){
                  console.log('erors and stuff');
                  console.log( e );
                });
            }
        };
like image 935
Daniel Wood Avatar asked Mar 08 '16 06:03

Daniel Wood


1 Answers

Yes, its right. You have to set an HTTP Header. Have a look at:

https://github.com/enyo/dropzone/wiki/FAQ#how-to-show-an-error-returned-by-the-server

If you have an HTTP Error and want to show it in the hover error message of DropzoneJS you can:

myDropzone.on("error", function(file, message, xhr) {
    var header = xhr.status+": "+xhr.statusText;
    $(file.previewElement).find('.dz-error-message').text(header);
  });

(You need jQuery for that code [$().find();])

The other way would be to return a JSON error message via PHP:

//define text for error message
$output['error'] = 'No Token';

//return right HTTP code
if( $error ){
    http_response_code (401);
}
else{
    http_response_code (200);
}

//set Content-Type to JSON
header( 'Content-Type: application/json; charset=utf-8' );
//echo error message as JSON
echo json_encode( $output );
like image 199
KIMB-technologies Avatar answered Oct 17 '22 02:10

KIMB-technologies