0

Trying to make an photo uploader using Intervention\Image I have this in ReportController@store :

public function SavePhoto($photo){
        $file_ext = $photo->getClientOriginalExtension();
        $file_name = uniqid();
        $photo_name = $file_name. '.' . $file_ext;
        $path = public_path('uploads/photos/' . $photo_name);
        Image::make($photo)->resize(300, null, function ($constraint) {
            $constraint->aspectRatio();
        })->save($path);
        return $photo_name;
    }

public function store(Request $request)
    {

        $observation = new Observation();
        $observation->content = $request['Observation'];
        $observation->status_id = $request['Status'];
        $photo = Input::file('photo');

        foreach ($photo as $p){
            $this->SavePhoto($p);

        }

I am so confused of how to call the SavePhoto() method for all photos input.

1 Answer 1

1

According to the Laravel API docs use the allFiles() method to get the files of $request.

$photos = $request->allFiles();
foreach ($photos as $photo){
    $this->SavePhoto($photo);
}

I haven't tried it though. :)
its the equivalent of getting the $_FILES array

foreach($_FILES as $photo) {
    $this->SavePhoto($photo);
}
Sign up to request clarification or add additional context in comments.

5 Comments

I tried it but but $photos is returning an empty array :- $photo = Input::allFiles(); return $photo; ==> {"photo":{}}
I corrected the mistake in my code. $_FILES is the name of the superglobal. Is the $_FILES array empty? If not, you check if you submitted it correctly and the data arrives.
I changed my code, the allFiles() method is part of Request object.
Still having a empty return -> $photo = $request->allFiles(); return $photo; result=> {"photo":{}} This is my input code : <input name="photo" type="file" multiple>
could you show the dump of $_FILES in your target script?

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.