0

i tried to upload more than one image in database by doing this in controller:

public function store(request $request) {

     $pictures=[];
    $input=$request->all();
    if($file=$request->file('images'))  $pictures[]=$request->file('images');
    if($file=$request->file('image1'))  $pictures[]=$request->file('image1');
    if($file=$request->file('image2'))  $pictures[]=$request->file('image2');
    if($file=$request->file('image3'))  $pictures[]=$request->file('image3');
    if($file=$request->file('image4'))  $pictures[]=$request->file('image4');
    if($file=$request->file('image5'))  $pictures[]=$request->file('image5');
    if($file=$request->file('image6'))  $pictures[]=$request->file('image6');

     foreach($pictures as $file)


    for($name=0;$name<=7;$name++)
    {
        $name=$file->getClientOriginalName();
    }
        $file->move('image',$name);

        $input['images']=$name;
        $input['image1']=$name;
        $input['image2']=$name;
        $input['image3']=$name;
        $input['image4']=$name;
        $input['image5']=$name;
        $input['image6']=$name;




  Detail::create($input);

    return redirect('/');


}

It takes images form form and stores the selected images in public/image folder but in database it stores all the images with same image name. and while displaying also it displays the same image many time.

I know guys here have solution to this and may have better idea. so please help me out in this. thanks in advance.

The above done method was not appropriate for me so i did this in my controller

     public function uploadSubmit(request $request)
{
    // Coming soon...
    $data=$request->all();
     $imagename =[];
    $i = 0;
    $files =Input::file('images');
    foreach($files as $file){
        $extension = $file->getClientOriginalExtension();
        $imagename[$i] = 'post'.str_random(10).'.jpg';
        $destinationPath =  'assets/posts';
        $file->move($destinationPath, $imagename[$i]);
        $i++;
    }
    Detail::create($files);
        return redirect('/');
    }

And in route:

    Route::resource('/details','DetailController');

Now i am getting an error like this:FatalThrowableError in DetailController.php line 43: Call to a member function getClientOriginalName() on array. Can anyone point the problem here. thanks.

2
  • What version of Laravel are you doing this for? Would help with providing a proper request example. You would send all of the images as an array to PHP using the same name. Are you POSTING to PHP through a standard form or javascript? Commented Feb 26, 2017 at 8:43
  • I am using laravel 5.4.12. Commented Feb 26, 2017 at 8:47

1 Answer 1

1

So I would setup a custom Request to validate the uploads are images and then you can upload them directly in your controller as shown in the examples below.

app\Http\Requests

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ImageRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {

        // Logic to authorize the request
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            // Any other required laravel validations
        ];

        if (is_array($this->request->get('images'))):
            foreach ($this->request->get('images') as $key => $val):
                if($key == 0)  continue;
                $rules['images.' . $key] = 'required|image';
            endforeach;
        endif;
        return $rules;
    }
}

app\Http\Controllers\ImageController

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\ImageRequest;

class ImageController extends Controller
{
    public function store(ImageRequest $request) {  
        $uploadedImagesPath = array();
        foreach($request->input("images") as $key => $image):
            $thisFile = $request->file("images")[$key];
            if($thisFile->isValid()):

                // Get original file name 
                $imageName = $thisFile->getClientOriginalName();                

                // Upload file to default Laravel configured upload location
                $uploadedImagesPath[$key] = $thisFile->storeAs('images', $imageName); 
                // Function was in your example code
                // Detail::create($request->file("images"));

            endif;
        endforeach;
        return redirect('/');
    }

}

You can read more about the topic on Laravel's site

https://laravel.com/docs/5.4/requests#retrieving-uploaded-files

https://laravel.com/docs/5.4/validation#form-request-validation

Sign up to request clarification or add additional context in comments.

3 Comments

i did as you suggested, but browser threw error like this: ReflectionException in RouteSignatureParameters.php line 25: Class Groomer\Http\Request\ImageRequest does not exist
Change use Groomer\Http\Requests\ImageRequest; to use app\Http\Requests\ImageRequest; or whatever class your app is named under - Forgot to change that from an example project I grabbed from.
I also made another change to the controller for the StoreAs function to work properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.