-1

Let's say that I want to upload 3 images a.jpeg, b.jpeg and c.jpeg

I've successfully selected the images that i want to upload. Sent them to the Controller and checked with print_f() to see if they're actually to the controller. They were.

Then when I've checked my img folder only c.jpeg were successfully uploaded while rest are not.

What is the reason of this?

// AdminController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Redirect;
use Image;

// Route 
  Route::post('/upload', 'AdminController@store')->name('upload');

// AdminController@store:

    public function store(Request $request){
      if ($request->hasfile('images')) {
        foreach ($request->images as $image) {
          $name = time() . '.' . $image->getClientOriginalExtension();
          print_r($name."<br>"); // to see if they're actually passed.
          Image::make($image)->save(public_path('img/new/'. $name));
        }

      }
      return('done');
    }


    <form action="{{ route('upload') }}" method="post" enctype="multipart/form-data">
      <div class="form-group">
        <label for="exampleInputFile">File input</label>
        <input type="file" name="images[]" id="exampleInputFile" multiple />
      </div>
      {{ csrf_field() }}
      <button type="submit" class="btn btn-default">Submit</button>
    </form>
cemilakkoc
  • 43
  • 7
  • 1
    Can you please change time() with something else, like use random string function for that and try to store it after that. I am sure thats causing the issue. – Wasif Iqbal Jul 02 '19 at 18:13
  • Mate, thank you so much. Changed it into rand() and it's fixed. – cemilakkoc Jul 02 '19 at 18:19
  • No worries. But still rand is not the solution. As at some point you have to read that image or file then you will never know how to capture that image dynamically unless you save the rand to database too. So instead use incremental name or associate id of that product or whatever crud operation you are saving image for. – Wasif Iqbal Jul 04 '19 at 20:27

1 Answers1

1

Problem was that I was using the time() function and not rand() or any similar function that returns a random value.

So the code shoud look like this now:

public function store(Request $request){
  if ($request->hasfile('images')) {
    foreach ($request->images as $image) {
      $name = rand() . '.' . $image->getClientOriginalExtension();
      print_r($name."<br>"); // to see if they're actually passed.
      Image::make($image)->save(public_path('img/new/'. $name));
    }

  }
  return('done');
}
cemilakkoc
  • 43
  • 7