Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to process file uploaded via Dropzone in Drupal?

Recently I have been trying to process files uploaded via Dropzone.js in Drupal. I am not using the Drupal API form stuff, just a plain HTML code to generate Dropzone, but I want to process uploaded files using Drupal functions (file_save_upload(), in particular). However, I am not able to "catch" the uploaded file using the function.

like image 381
petiar Avatar asked Dec 30 '25 21:12

petiar


1 Answers

For some reason, the $_FILES array generated by Dropzone, looks like this:

Array   (
    [files] => Array
        (
            [name] => 5bcb9c.jpg
            [type] => image/jpeg
            [tmp_name] => /private/var/tmp/phpgZHmT8
            [error] => 0
            [size] => 273794
        )   )

However, Drupal's function file_save_upload() is awaiting the $_FILES array in this structure:

Array
(
    [files] => Array
        (
            [name] => Array
                (
                    [files] => 5bcb9c.jpg
                )

            [type] => Array
                (
                    [files] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [files] => /private/var/tmp/php4ebqBs
                )

            [error] => Array
                (
                    [files] => 0
                )

            [size] => Array
                (
                    [files] => 210075
                )

        )

)

so what I had to do was to change the $_FILES variable (yes, I know!) so it matches the structure required by Drupal. It works now, although I do not quite understand why Dropzone generates another structure and Drupal requires other structure of the $_FILES variable. If anyone can elaborate on this, I would be glad. Nevertheless, my solution works.

like image 95
petiar Avatar answered Jan 04 '26 15:01

petiar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!