Sopamo / laravel-filepond

Laravel backend module for filepond uploads
MIT License
203 stars 56 forks source link

Added foreach so we can retrieve $serverID from multiple input #2

Closed xerod closed 5 years ago

xerod commented 5 years ago

Solved my #1 problem when using multiple input or allowMultiple options and only retrieve one $serverID when using $request->get('file'). Just to make sure, you have to re-configure you Filepond.setOptionsto:

FilePond.setOptions({
    name: 'file[]',
    server: window.location.protocol+'//'+window.location.hostname+'/filepond/api/process',
})
Sopamo commented 5 years ago

Thank you for your pull request! Could you have a look at the code again?

xerod commented 5 years ago

This works fine, my first assumption is: even it end the function, the second one still executed because filepond processed it one by one (if it's two images, filepond would process it two times). This should tell:

image image

I added the foreach at FileondController.php so the upload function can process the array even it's returning at the first loop. If i removed it (using your original code), it's basically give an exception like this:

image

Bottom line: the foreach didn't do multiple loop, it's basically extract the array that filepond give (one at a time).

As long you're using the array name file[] for the input (like what they did at filepond PHP boilerplate), you're good to go. I don't know if this is the most efficient way or not. It's still nice to have this package already, saved more time than it should. Thanks for replying tho

Sopamo commented 5 years ago

Okay, that makes more sense now. I was wondering how filepond would handle the response for multiple files. If you like, you could refactor your code so it just gets the first element instead of looping over the array (because we aren't actually looping, as you said). If you don't have the time, I can merge your PR and do it myself.

xerod commented 5 years ago

do you mean like this?:

    public function upload(Request $request)
    {
        $file = $request->file('file')[0];

        $filePath = tempnam(config('filepond.temporary_files_path'), "laravel-filepond");
        $filePathParts = pathinfo($filePath);

        if(!$file->move($filePathParts['dirname'], $filePathParts['basename'])) {
            return Response::make('Could not save file', 500);
        }
        return Response::make($this->filepond->getServerIdFromPath($filePath), 200);
    }

looks more cleaner to me.

Sopamo commented 5 years ago

Perfect, thanks!

xerod commented 5 years ago

Don't forget to update your readme.md @Sopamo

bfuchs commented 4 years ago

This change breaks functionality for me when uploading single files. I get the following error in /src/Http/Controllers/FilepondController.php:

Cannot use object of type Illuminate\Http\UploadedFile as array

$request->file('file') returns an no array.