# Valid Stream Upload File

**URL:** <https://discourse.laminas.dev/t/valid-stream-upload-file/669>\
**Category:** Mezzio\
**Tags:** expressive\
**Created:** [August 2, 2018, 4:02pm UTC](https://discourse.laminas.dev/t/valid-stream-upload-file/669 "2018-08-02T16:02:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![coffeecore](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/coffeecore/32/365_2.png) [@coffeecore](https://discourse.laminas.dev/u/coffeecore)\
**Post date:** [August 2, 2018, 4:02pm UTC](https://discourse.laminas.dev/t/valid-stream-upload-file/669/1 "2018-08-02T16:02:03Z")

</div>

Hi,

I try to use Zend Expressive, but I don’t understand how use FileRenameUpload and I can’t valid my form.

```
if ($request->getMethod() === 'POST') {
            $this->imageForm->setData(array_merge($request->getParsedBody(), $request->getUploadedFiles()));
            if ($this->imageForm->isValid()) {
                dd($this->imageForm->getData());
            }
        }

```

I put this in my form class to move file after upload :

```
public function getInputFilterSpecification()
    {
        return [
            [
                'name' => 'file',
                'required' => true,
                'filters' => [
                    [
                        'name' => 'FileRenameUpload',
                        'options' => [
                            'target' => __DIR__.'../../../data/upload',
                            'useUploadName' => true,
                            'useUploadExtension' => true,
                            'overwrite' => true,
                            'randomize' => true
                        ]
                    ]
                ],
            ]
        ];
    }

```

But when I do isValid on form, just this error :

> ```
> "fileUploadFileErrorNoFile" => "File was not uploaded"
> 
> ```

I don’t understand how use a Stream and valid it

I do right ? I readed doc but nothing about upload and valid form for Expressive.

Thanks

---

<div class="post-metadata">

**Author:** ![matthew](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matthew/32/14_2.png) [@matthew](https://discourse.laminas.dev/u/matthew)\
**Post date:** [August 2, 2018, 4:07pm UTC](https://discourse.laminas.dev/t/valid-stream-upload-file/669/2 "2018-08-02T16:07:31Z")

</div>

The various file validators and input types do not currently work with PSR-7, which is what Expressive uses. You will have to handle those separately currently.

We’re working on a new validation/input filter component ([spec](https://discourse.zendframework.com/t/rfc-new-validation-component/208)), and will likely tackle file upload validation, filtering, etc. in that component.

---

<div class="post-metadata">

**Author:** ![samsonasik](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/samsonasik/32/777_2.png) [@samsonasik](https://discourse.laminas.dev/u/samsonasik)\
**Post date:** [August 2, 2018, 4:38pm UTC](https://discourse.laminas.dev/t/valid-stream-upload-file/669/3 "2018-08-02T16:38:55Z")

</div>

For now, you can use `Psr7Bridge` like this:

```php
use Zend\Psr7Bridge\Psr7ServerRequest;

$zendRequest = Psr7ServerRequest::toZend($request);
$this->imageForm->setData(\array_merge_recursive(
        $zendRequest->getPost()->toArray(),
        $zendRequest->getFiles()->toArray()
));

```

---

<div class="post-metadata">

**Author:** ![Ralf\_Eggert](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ralf_eggert/32/298_2.png) [@Ralf\_Eggert](https://discourse.laminas.dev/u/Ralf_Eggert)\
**Post date:** [August 2, 2018, 5:23pm UTC](https://discourse.laminas.dev/t/valid-stream-upload-file/669/4 "2018-08-02T17:23:36Z")

</div>

Funny enough, I was just going to write a similar question. Saved some time now though I did not find a ready-to-use solution.

---

<div class="post-metadata">

**Author:** ![coffeecore](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/coffeecore/32/365_2.png) [@coffeecore](https://discourse.laminas.dev/u/coffeecore)\
**Post date:** [August 3, 2018, 12:41pm UTC](https://discourse.laminas.dev/t/valid-stream-upload-file/669/5 "2018-08-03T12:41:08Z")

</div>

Hi,

thanks you for your fast replies. (and for your blog samsonasik 😃 )

I didn’t read anything about non-fonctinal validators and inputfilters.

I took Psr7Bridge and it works like a charm.

There is a cleaner how to way to do this ?

You’re welcome Ralf.

---

<div class="post-metadata">

**Author:** ![Alexander\_Romanenko](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/alexander_romanenko/32/537_2.png) [@Alexander\_Romanenko](https://discourse.laminas.dev/u/Alexander_Romanenko)\
**Post date:** [December 19, 2018, 1:13am UTC](https://discourse.laminas.dev/t/valid-stream-upload-file/669/6 "2018-12-19T01:13:08Z")

</div>

As of zend-filter 2.9, it can now be done without a bridge.  
[https://docs.zendframework.com/zend-filter/file/](https://docs.zendframework.com/zend-filter/file/)  
Look for PSR-7 section.

Configuration can now be:

```auto
use Zend\Diactoros\StreamFactory;
use Zend\Diactoros\UploadedFileFactory;

public function getInputFilterSpecification()
    {
        return [
            [
                'name' => 'file',
                'required' => true,
                'filters' => [
                    [
                        'name' => 'FileRenameUpload',
                        'options' => [
                            'target' => __DIR__.'../../../data/upload',
                            'useUploadName' => true,
                            'useUploadExtension' => true,
                            'overwrite' => true,
                            'randomize' => true,
                            'stream_factory' => new StreamFactory(),
                            'upload_file_factory' => new UploadedFileFactory(),
                        ]
                    ]
                ],
            ]
        ];
    }

```

---

<div class="post-metadata">

**Author:** ![samsonasik](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/samsonasik/32/777_2.png) [@samsonasik](https://discourse.laminas.dev/u/samsonasik)\
**Post date:** [December 27, 2018, 12:52pm UTC](https://discourse.laminas.dev/t/valid-stream-upload-file/669/7 "2018-12-27T12:52:27Z")

</div>

that may only works for filter, for validation, eg: Size, MimeType, it still needs Psr7bridge, refs [https://github.com/zendframework/zend-form/issues/227](https://github.com/zendframework/zend-form/issues/227)

---

<div class="post-metadata">

**Author:** ![Alexander\_Romanenko](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/alexander_romanenko/32/537_2.png) [@Alexander\_Romanenko](https://discourse.laminas.dev/u/Alexander_Romanenko)\
**Post date:** [December 27, 2018, 3:53pm UTC](https://discourse.laminas.dev/t/valid-stream-upload-file/669/8 "2018-12-27T15:53:16Z")

</div>

Ah, did not realize there were still more. Sorry for oversight, and thank you for testing. Will try to get the rest of them done.
