Search code examples
phpfopenguzzlelaragon

Why is PHP unable to open a stream, permission denied using mode w+ fopen?


I am on Windows 10. I'm using the laragon dev environment, which wraps apache.

I am trying to run the following guzzle call to download a file:

 $downloadClient = new Client(['base_uri' => 'https://vendor/region']);
 $downloadResponse = $downloadClient->request('GET','endpoint/file_url.txt',['sink' => __DIR__]);

I keep getting the following in return:

Fatal error: Uncaught RuntimeException: Unable to open C:\laragon\www\act using mode w+: fopen(C:\laragon\www\act): failed to open stream: Permission denied in C:\laragon\www\act\vendor\guzzlehttp\psr7\src\functions.php:303 Stack trace: #0 [internal function]: GuzzleHttp\Psr7{closure}(2, 'fopen(C:\larago...', 'C:\laragon\www\...', 311, Array) #1 C:\laragon\www\act\vendor\guzzlehttp\psr7\src\functions.php(311): fopen('C:\laragon\www\...', 'w+') #2 C:\laragon\www\act\vendor\guzzlehttp\psr7\src\LazyOpenStream.php(37): GuzzleHttp\Psr7\try_fopen('C:\laragon\www\...', 'w+') #3 C:\laragon\www\act\vendor\guzzlehttp\psr7\src\StreamDecoratorTrait.php(31): GuzzleHttp\Psr7\LazyOpenStream->createStream() #4 C:\laragon\www\act\vendor\guzzlehttp\psr7\src\StreamDecoratorTrait.php(136): GuzzleHttp\Psr7\LazyOpenStream->__get('stream') #5 C:\laragon\www\act\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(395): GuzzleHttp\Psr7\LazyOpenStream->write(' in C:\laragon\www\act\vendor\guzzlehttp\psr7\src\functions.php on line 303

I have tried granting "Full Control" to my user account, which is what Laragon is running as. I've also tried granting all to IUSR and "all users" on the directory with now results.

Any help is appreciated.


Solution

  • You're passing __DIR__ as the "sink" where to write the file. That's a directory. You can't write data to a directory like it was a file. Try using the name of a file.

    $downloadClient = new Client(['base_uri' => 'https://vendor/region']);
    $downloadResponse = $downloadClient->request('GET','endpoint/file_url.txt',
          ['sink' => "file_url.txt"]);