cariboucreative's avatar

Multipart file upload to S3

Hey guys! I'm trying to do a multipart file upload to an S3 bucket, but i'm coming across a few issues. The directories are being created and the thumbnail is being uploaded, but the main images are not uploading and there is a strange file appearing alongside the directory. This is what I have:

$files = $request->file('images');
            $count = 0;
            foreach($files as $file) {

                $img = \Image::make($file);
                //resize width to 910px and preserve aspect ratio
                //save images and thumbs
                if($count == 0){
                    $img->fit(1080, 578)->encode('jpg', 85);
                    $disk->put('/images/'.$slug.'/'.$slug.'-'.$count.'.jpg', $img->__toString());
                    $img->fit(400, 267)->encode('jpg', 85);
                    $disk->put('/images/'.$slug .'/thumbnails/'.$slug.'-thumb.jpg', $img->__toString());
                    $img->destroy();
                    ++$count;
                }else{
                    $img->fit(1080, 578)->encode('jpg', 85);
                    $disk->put('/images/'.$slug.'/', $img->__toString());
                    ++$count;
                }

I'm basically looping through the images array, creating and uploading a thumb from the first image and then uploading each image (with a little intervention manipulation).

i've dumped the images array to make sure it has more than one image. There is a strange file being uploaded to the bucket with each image also. It's appear to have encoded data from the images in it. Not quite sure where to go to from here as there are not a lot of resources about L5 multipart uploads to S3 buckets.

Any help would be much appreciated.

EDIT: $disk is just a variable holder for Storage::disk(s3) and $slug is just another field sluggified

0 likes
4 replies
deltasolutions's avatar

Hello, the $disk->put part in the else block doesn't specify a filename

1 like
cariboucreative's avatar

Hey! Ya thanks! I spotted that alright. I had been fiddling with the path before I posted here and forgot to revert back. It still gave me errors. The problem was with the way I was stringifying the file in the put method. I fixed it with

$payload = (string)$img->encode();
$disk->put('/projects/'.$slug.'/'.$slug.'-'.$count.'.jpg', $payload);

That I came across on another thread here. Thanks for the spot though! You have a good eye for a needle. :D

ttson24's avatar

Hi you!

i am used client

<div class="form-group">
                            {!!Form::label('detail', 'Images')!!}                            
                            <div class="row fileupload-buttonbar">             
                                <div class="col-lg-6">
                                    <!-- The fileinput-button span is used to style the file input field as button -->
                                    <span class="btn btn-success fileinput-button"> <i
                                        class="glyphicon glyphicon-plus"></i> <span>Add files...</span> <input
                                        type="file" name="files[]" id="fileUp" multiple  >
                                    </span>
                                    <button type="button" class="btn btn-primary start " id="upload">
                                        <i class="glyphicon glyphicon-upload"></i> <span>Start upload</span>
                                    </button>
                                    <!-- The global file processing state -->
                                    <span class="fileupload-process"></span>
                                </div>
                                <!-- The global progress state -->
                                <div class="col-lg-5 fileupload-progress fade">
                                    <!-- The global progress bar -->
                                    <div class="progress progress-striped active" role="progressbar"
                                        aria-valuemin="0" aria-valuemax="100">
                                        <div class="progress-bar progress-bar-success" style="width: 0%;"></div>
                                    </div>
                                    <!-- The extended global progress state -->
                                    <div class="progress-extended"> </div>
                                </div>
                            </div>
                            <!-- The table listing the files available for upload/download -->
                            <table role="presentation" class="table table-striped" >
                                <tbody class="files"></tbody>
                            </table>                           
                        </div>

$input = Input::all(); but i do not see value of control <input type="file" name="files[]" id="fileUp" multiple > how do i do. help me!!

deltasolutions's avatar

@cariboucreative , is your form of the enctype multipart/form-data ?

{!!Form::open(array('autocomplete' => 'off','class' => 'form-horizontal', 'enctype' => "multipart/form-data", 'id' => 'aanvragen-update-form', 'method' => 'post'))!!}

Please or to participate in this conversation.