Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple files in POST #13

Closed
sladdky opened this issue Jun 23, 2018 · 6 comments
Closed

Multiple files in POST #13

sladdky opened this issue Jun 23, 2018 · 6 comments
Milestone

Comments

@sladdky
Copy link

sladdky commented Jun 23, 2018

If you try to upload two files in POST and one is empty (in case of both being empty, the issue occurs as well, but I'm not sure now),

Contributte\Psr7\Psr7ServerRequest::normalizeNetteFiles

will throw exception

InvalidArgumentException('Invalid value in files specification').

If you try to upload multiple files in POST as array (type='file[1]', type='file[2]'), it throws exception again because parameter array $files contains inner arrays.

intended or am I doing anything wrong?

for now I modified method of Contributte\Psr7\Psr7ServerRequest to accept recursive arrays and ignore empty files:

public static function normalizeNetteFiles(array $files)
    {
        $normalized = [];

        foreach ($files as $file) {
            if ($file instanceof FileUpload) {
                $normalized[] = new Psr7UploadedFile(
                    $file->getTemporaryFile(),
                    intval($file->getSize()),
                    $file->getError(),
                    $file->getName(),
                    $file->getContentType()
                );
            } else if (is_array($file)) {
                $normalized = array_merge($normalized, self::normalizeNetteFiles($file));
            } else if ($file === null) {
                continue;
            } else {
                throw new InvalidArgumentException('Invalid value in files specification');
            }
        }

        return $normalized;
    }

@f3l1x
Copy link
Member

f3l1x commented Jun 24, 2018

Hi, thanks for an issue. I need to check it manually. Could you please describe when file is empty?

@sladdky
Copy link
Author

sladdky commented Jun 24, 2018

This package is being used in apitte. Here's my code of a controller and a form.

/**
 * @Controller
 * @ControllerPath("/test")
 */
final class TestController extends BaseController
{
    /**
     * @Path("/")
     * @Method("POST")
     */
    public function index(ApiRequest $request, ApiResponse $response)
    {
        $files = [];

        foreach ($request->getUploadedFiles() as $file) {
            $files[] = $file;
        }

        return $response->withStatus(420, strval(count($request->getUploadedFiles())));
    }
}
<form action="http://localhost/test" method="POST" enctype="multipart/form-data">
    <input type="file" name="file1">
    <input type="submit">
</form>

if sent without image, exception is thrown that cannot be caught in the controller.

By empty file I meant empty field, thus no file being send.

@f3l1x
Copy link
Member

f3l1x commented Jun 28, 2018

You be fixed. Could you please test it @Odoaker-Alaric ?

@sladdky
Copy link
Author

sladdky commented Jun 28, 2018 via email

@f3l1x
Copy link
Member

f3l1x commented Jun 28, 2018

Yep, it will be in apitte v0.4. We will release v0.3 with some tiny improvements and then v0.4 will be with PHP 7.1

You can trick it with contributte/psr-7-http-message: "dev-master as 0.3.

It should use dev-master branch and for the rest of the packages it looks like same old v0.3.

@sladdky
Copy link
Author

sladdky commented Jun 28, 2018

Cool trick, so far so good.
Works as intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants