bpflugrad's avatar

File Upload Failing with "Invalid stream or file provided for UploadedFile"

Hello,

First post here. I've done a lot of searching but can't find a solution.

I'm trying to make a simple file upload POST route. This will be accessed by an Angular2 webapp. The problem occurs in the app and when using Postman.

Postman payload:

POST /API/v2/media HTTP/1.1
Host: app.localhost.com
Authorization: Bearer [REDACTED]
Cache-Control: no-cache
Postman-Token: [REDACTED]
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="photo_2017-06-06_19-45-10.jpg"
Content-Type: image/jpeg


------WebKitFormBoundary7MA4YWxkTrZu0gW--

Lumen.log:

[2017-10-24 02:23:38] lumen.ERROR: exception 'InvalidArgumentException' with message 'Invalid stream or file provided for UploadedFile' in C:\inetpub\wwwroot\app\server\vendor\zendframework\zend-diactoros\src\UploadedFile.php:84
Stack trace:
#0 C:\inetpub\wwwroot\app\server\vendor\symfony\psr-http-message-bridge\Factory\DiactorosFactory.php(118): Zend\Diactoros\UploadedFile->__construct(false, 103924, 0, 'photo_2017-06-0...', 'image/jpeg')
#1 C:\inetpub\wwwroot\app\server\vendor\symfony\psr-http-message-bridge\Factory\DiactorosFactory.php(94): Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory->createUploadedFile(Object(Symfony\Component\HttpFoundation\File\UploadedFile))
#2 C:\inetpub\wwwroot\app\server\vendor\symfony\psr-http-message-bridge\Factory\DiactorosFactory.php(57): Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory->getFiles(Array)
#3 C:\inetpub\wwwroot\app\server\vendor\laravel\passport\src\Guards\TokenGuard.php(107): Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory->createRequest(Object(Illuminate\Http\Request))
#4 C:\inetpub\wwwroot\app\server\vendor\laravel\passport\src\Guards\TokenGuard.php(90): Laravel\Passport\Guards\TokenGuard->authenticateViaBearerToken(Object(Illuminate\Http\Request))
#5 C:\inetpub\wwwroot\app\server\vendor\laravel\passport\src\PassportServiceProvider.php(267): Laravel\Passport\Guards\TokenGuard->user(Object(Illuminate\Http\Request))
#6 [internal function]: Laravel\Passport\PassportServiceProvider->Laravel\Passport\{closure}(Object(Illuminate\Http\Request))
#7 C:\inetpub\wwwroot\app\server\vendor\illuminate\auth\RequestGuard.php(56): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#8 C:\inetpub\wwwroot\app\server\vendor\illuminate\auth\GuardHelpers.php(49): Illuminate\Auth\RequestGuard->user()
#9 C:\inetpub\wwwroot\app\server\vendor\illuminate\auth\GuardHelpers.php(59): Illuminate\Auth\RequestGuard->check()
#10 C:\inetpub\wwwroot\app\server\app\Http\Middleware\Authenticate.php(38): Illuminate\Auth\RequestGuard->guest()
#11 C:\inetpub\wwwroot\app\server\vendor\illuminate\pipeline\Pipeline.php(148): App\Http\Middleware\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure), 'api')
#12 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#13 C:\inetpub\wwwroot\app\server\vendor\laravel\lumen-framework\src\Routing\Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#14 C:\inetpub\wwwroot\app\server\vendor\illuminate\pipeline\Pipeline.php(102): Laravel\Lumen\Routing\Pipeline->Laravel\Lumen\Routing\{closure}(Object(Illuminate\Http\Request))
#15 C:\inetpub\wwwroot\app\server\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(778): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#16 C:\inetpub\wwwroot\app\server\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(624): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#17 C:\inetpub\wwwroot\app\server\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(528): Laravel\Lumen\Application->handleFoundRoute(Array)
#18 C:\inetpub\wwwroot\app\server\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(781): Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}()
#19 C:\inetpub\wwwroot\app\server\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(534): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#20 C:\inetpub\wwwroot\app\server\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(475): Laravel\Lumen\Application->dispatch(NULL)
#21 C:\inetpub\wwwroot\app\server\public\index.php(28): Laravel\Lumen\Application->run()
#22 {main}  

When I look at Zend\Diactoros\UploadedFile I see:

...
if ($errorStatus === UPLOAD_ERR_OK) {
            if (is_string($streamOrFile)) {
                $this->file = $streamOrFile;
            }
            if (is_resource($streamOrFile)) {
                $this->stream = new Stream($streamOrFile);
            }

            if (! $this->file && ! $this->stream) {
                if (! $streamOrFile instanceof StreamInterface) {
                    throw new InvalidArgumentException('Invalid stream or file provided for UploadedFile');
                }
                $this->stream = $streamOrFile;
            }
        }
...

It's clear from the error message that $streamOrFile is being sent in as false.

At a loss as to why this would be happening. Any help would be appreciated.

Thanks!

0 likes
2 replies
bpflugrad's avatar
bpflugrad
OP
Best Answer
Level 1

Ok after tracing the issue all the way back to \SplFileInfo I found that IIS had write permission to C:\Windows\Temp, but not read permission. So the file would arrive properly and then not be readable into Zend\Diactoros\UploadedFile.

If anyone else runs into a 500 with "Invalid stream or file provided for UploadedFile" check permissions on the folder IIS/PHP is uploading to.

msyadav's avatar

I am facing the same issue using guzzlehttp with multipart and header(authorization). Without authorization it is working fine.

Please or to participate in this conversation.