Search code examples
laraveluploadintervention

Multiple file upload in laravel using intervention


<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Image;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Storage;

class TestController extends Controller
  {
 /**
  * Show the application dashboard.
  *
 * @return \Illuminate\Http\Response
  */
  public function index(Request $req)
 {

 if(isset($_POST['upload'])){
    $filename = $_FILES['imagefile']['name'];
   foreach($filename as $file){

   $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file);
    $location = public_path('/images/test/' . $withoutExt);
   $img = Image::make($file)->resize('720', '404')->save($location.'.jpg');
  }
  }}}

I have used the $img = Image::make(Input::file('imagefile'))->resize('720', '404')->save($location.'.jpg'); for single file and it is working fine but for multiple file uploading I am using the $file instead of using the Input then it shows the error Image source not readable


Solution

  • You define var as $filename = $_FILES['imagefile']['name']; so wouldn't that just get a string value? And string is not readable. Let's try with just $filename = $_FILES['imagefile']

    UPDATE

    You loop the string value from filename and it's not readable image.You need a few other things to work with image upload.

    if($req->hasfile('imagefile'))
    {
        foreach($req->file('imagefile') as $file)
        {
            $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file->getClientOriginalName());
            $location = public_path('/images/test/' . $withoutExt);
            $img = Image::make($file)->resize('720', '404')->save($location.'.jpg');
        }
    }
    

    And because you use Laravel, I suggest you to just use $req parameter to get your request.