Flex's avatar
Level 4

null value out come in the images input field in laravel 5.6

in my laravel application I am using dropzone programmatically to upload images. this is my Controller to store images in VehicleController

$photos = $request->file('file');
dd($photos);
        if (!is_array($photos)) {
            $photos = [$photos];
        }

        if (!is_dir($this->photos_path)) {
            mkdir($this->photos_path, 0777);
        }

        for ($i = 0; $i < count($photos); $i++) {
            $photo = $photos[$i];
            $name = sha1(date('YmdHis') . str_random(30));
            $save_name = $name . '.' . $photo->getClientOriginalExtension();//this is line 75
            $resize_name = $name . str_random(2) . '.' . $photo->getClientOriginalExtension();

            Image::make($photo)
                ->resize(250, null, function ($constraints) {
                    $constraints->aspectRatio();
                })
                ->save($this->photos_path . '/' . $resize_name);

            $photo->move($this->photos_path, $save_name);

            $upload = new Upload();
            $upload->filename = $save_name;
            $upload->resized_name = $resize_name;
            $upload->original_name = basename($photo->getClientOriginalName());
            $upload->save();
        }
        return Response::json([
            'message' => 'Image saved Successfully'
        ], 200);

and programatically jquery is

Dropzone.autoDiscover = false;

// Dropzone class:
var myDropzone = new Dropzone("div#my-dropzone", { url: '/form'});

routes

Route::post('form','VehicleController@store');

My blade file is

<div class="dropzone" id="my-dropzone"> 
    <div class="dz-message">

              <div class="col-xs-8">
                 <div class="message">
                    <p>Drop files here or Click to Upload</p>
                 </div>
              </div>
    </div>
    <div class="fallback">
        <input type="file" name="file" multiple>

    </div>
</div>

but with this div class

 <div class="fallback">
        <input type="file" name="file" multiple>

    </div>

images submitting out comes null value with dd(photos);

but without above div class="fallback" dropzone box is working but with single image uploading. then how can fix multiple images uploding null values outcomes?

0 likes
6 replies
ahmeddabak's avatar

Please make sure that you added the encoding type to your form

enctype="multipart/form-data"
<form action="action.php" method="post" enctype="multipart/form-data">

<!-- form input fields here -->
</form>
Loach's avatar

Why do you need a photo array? Don't dropzone just send them one at a time? When I use it I upload multiple files I just process "one image" in my store function. All the images will be sent there so why do you need to bother with an array? Just have code to process one image and they all will be sent to that function.

Flex's avatar
Level 4

@Loach actually I cant understand what did you mean?Why do you need a photo array....

Loach's avatar

Sorry I misunderstood your problem.

webtrickshome's avatar

First of all, define the field name as array i.e.

to allow multiple file uploads. Wrapyour form fields with form tag with attribute
enctype="multipart/form-data"
Here's a tutorial on laravel file uploads which you can use as a guide.
1 like

Please or to participate in this conversation.