Ap3twe's avatar

Image Intervention changing file upload to multiple files

I am using Intervention to upload files. Now I want to have the files upload as an array of files.(multiple) How do I go about it? I tried lab_quote[] but is not working. Or is it best practice to have each file as a separate column in the db?

 if ($request->hasFile('lab_quote')){
        // get file Name
        $quotenameWithExt = $request->file('lab_quote')->getClientOriginalName();
        //The image path and name
        $quotefilename = pathinfo($quotenameWithExt, PATHINFO_FILENAME);
        // get the exxtension
        $quoteextension = $request->file('lab_quote')->getClientOriginalExtension();
        // Name append a time stamp
        $labquotefileNameToStore = $quotefilename.'_' .time(). '.' . $quoteextension;
        // Move the file to the folder
        // $stlpath = $request->file('stl_image')->storeAs('public/uploads', $stlfileNameToStore);
        $qtlpath = $request->file('lab_quote')->storeAs('public/uploads/thumbnails', $labquotefileNameToStore);
        // Resize
        $quote = public_path('storage/uploads/thumbnails'. $labquotefileNameToStore);
        $qto = Image::make($_FILES['lab_quote']['tmp_name']);
        $qto->resize('443','313');
        $qto->save($quote);
        
    }
$project->fill(['lab_quote' => $labquotefileNameToStore]);
project->save();
0 likes
5 replies
Sergiu17's avatar

Exactly the same code you have, but in a foreach loop. And you may need to create a new table specifically for files, because now you save more than one.

foreach($request->get('files') as $file) {
    // rename
    // prepare file
    // resize
    // store it
}
Sergiu17's avatar

$file - is just a temporary variable in the loop - representing the file.

lab_quote[] - you declare your field as an array in your HTML

// controller 
foreach($request->get('lab_quote') as $file) {
    // 
}
Ap3twe's avatar

I am getting an error Invalid argument supplied for foreach(). On the datatype in the DB, I am using string $table->string('lab_quote[]')->nullable();

Sergiu17's avatar
Sergiu17
Best Answer
Level 60

@AP3TWE - no, you can't use $table->string('lab_quote[]')->nullable(); - if you want to add more images to a Model, then you have to create another table and create relationship (one to many) between the tables. Another option is to store name of the files in a comma separate string format, something like this: fileName1, fileName2, fileName3

Google for laravel upload multiple files

https://appdividend.com/2018/02/05/laravel-multiple-images-upload-tutorial/ this is one of the results, which may help.

Please or to participate in this conversation.