brandonsavage / Upload

File uploads with validation and storage strategies
MIT License
1.67k stars 315 forks source link

Multiple input file with array in name attribut #115

Open fdirson-OpenGateway opened 2 years ago

fdirson-OpenGateway commented 2 years ago

Hello,

If you have this form:

<form method="post" action="#" enctype="multipart/form-data">
    <input type="file" class="form-control" name="photo_name[0]">
    <input type="file" class="form-control" name="photo_name[1]">
</form>

You can't use this component:

$file = new \Upload\File('photo_name', $this->localStorage);

SplFileInfo::__construct() expects parameter 1 to be a valid path, array given

OR

$file = new \Upload\File('photo_name[0]', $this->localStorage);

Cannot find uploaded file identified by key: photo_name[0]

var_dump($_FILES);

array (size=1) 'photo_name' => array (size=5) 'name' => array (size=1) 0 => string '123.jpeg' (length=43) 'type' => array (size=1) 0 => string 'image/jpeg' (length=10) 'tmp_name' => array (size=1) 0 => string 'C:\wamp64\tmp\phpDF44.tmp' (length=25) 'error' => array (size=1) 0 => int 0 'size' => array (size=1) 0 => int 1443745

I don't see anything about this behaviour.

Temporarily i hack \Upload\File constructor like this:

public function __construct($key, \Upload\Storage\Base $storage, ?int $index = null)
    {
        if (! isset($_FILES[$key])) {
            throw new \InvalidArgumentException("Cannot find uploaded file identified by key: $key");
        }
        $this->storage = $storage;
        $this->validations = array();
        $this->errors = array();
        if (is_null($index)) {
            $this->originalName = $_FILES[$key]['name'];
            $this->errorCode = $_FILES[$key]['error'];
            parent::__construct($_FILES[$key]['tmp_name']);
        } else {
            $this->originalName = $_FILES[$key]['name'][$index];
            $this->errorCode = $_FILES[$key]['error'][$index];
            parent::__construct($_FILES[$key]['tmp_name'][$index]);
        }
    }

It seems to work, i hope you will fix it if you can for next release.

Best regards,