Home » Php » javascript – loop, json call, and doctype fo php file upload

javascript – loop, json call, and doctype fo php file upload

Posted by: admin February 25, 2020 Leave a comment

Questions:

I need to upload multiple files using dropzone and put them in my action function in PHP. I need to call the api from dropzone and do a loop for all files uploaded to be directed to the correct file type, and then echo it out as success.

But I get an error every time I uncomment document_type => $doc_choice and I have the following code. I need help to figure this out:

PHP

if (isset($_FILES['file']) && !empty($_FILES['file']) && !empty($_FILES['file']['name'][0]))
    for($i=0; $i<$total; $i++) { //foreach($files as $file) {
        \Model_Log::append('dropzone', print_r($_FILES, true));


        $myFile = $_FILES['file'];
        $result_id = \Model_Document::addS3(\Model_Account::getCaseId(), array(
                            'name' => $myFile["name"][0],
                            'tmp_name' => $myFile["tmp_name"][0],
                            'type' => $myFile["type"][0],
                            'size' => $myFile["size"][0],
                            'error' => $myFile["error"][0]
                            //'document_type' => $doc_choice

         ));
     }

JQuery:

                    $("#uploader").dropzone({
                        url: "/upload.php",
                        maxFilesize: 4,
                        init: function() {
                            this.on("success", function(file, response) {
                                var obj = jQuery.parseJSON(response)
                        })
                    }
                });
How to&Answers:

As you have not shared your error. So I am guessing it must be because of ‘document_type’ field in the database table. Ther field must be non-null or the default value is not set.

You can suppress the error by allowing null value or set default value. Thought I don’t recommand it. Document type is very much important information regarding a file.