0

What I have

I created a very simple multiple image uploader in Laravel 6. The code of the view and the controller are the following:

View:

<form action="{{ route('create') }}" method="POST" enctype="multipart/form-data">
    @csrf
    <input name="images[]" type="file" multiple="multiple" />
</form>

Controller:

function create(Request $request) {
    $this->validate($request, [
        'images.*' => 'image',
    ]);

    foreach($request->images as $image) {
        // $filename = 'IMG_1921.jpg'
        $filename = $image->getClientOriginalName();

        // $path = 'uploads/IMG_1921.jpg'
        $path = $image->storeAs('uploads', $filename);

        // $absolutePath = 'C:\htdocs\laravel-test\storage/uploads/IMG_1921.jpg'
        $absolutePath = sprintf('%s/%s', storage_path(), $path);

        // crash here
        // $data = getimagesize($absolutePath); 
    }

    return 'ok';
}

The problem

All works fine until I uncomment the line of the getimagesize($absolutePath) function. I also tried with filesize($absolutePath) and the same error is given:

Argument 1 passed to Facade\FlareClient\Context\RequestContext::Facade\FlareClient\Context{closure}() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, array given

I don't understand why this error is showed if getimagesize() doesn't use any UploadedFile object.

More information about the error:

enter image description here

2
  • try dd($absolutePath) and comment the result Commented Sep 10, 2019 at 11:14
  • dd($absolutePath) returns "C:\htdocs\laravel-test\storage/uploads/IMG_1921.jpg". gettype($absolutePath) returns string. Commented Sep 10, 2019 at 11:49

2 Answers 2

1

I found the problem: the app folder is missed in the absolute path. It should be C:\htdocs\laravel-test\storage/app/uploads/IMG_1921.jpg instead C:\htdocs\laravel-test\storage/uploads/IMG_1921.jpg.

So, the solution is to add /app here:

$absolutePath = sprintf('%s/app/%s', storage_path(), $path);
Sign up to request clarification or add additional context in comments.

Comments

0

Try doing this inside your foreach loop:

$data = getimagesize($image->photo->path());

Comments

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.