# Output 5Mb JSON error: slim/slim/Slim/Http/Stream.php on line 385

**URL:** <https://discourse.slimframework.com/t/output-5mb-json-error-slim-slim-slim-http-stream-php-on-line-385/935>\
**Category:** Questions\
**Created:** [November 23, 2016, 12:18pm UTC](https://discourse.slimframework.com/t/output-5mb-json-error-slim-slim-slim-http-stream-php-on-line-385/935 "2016-11-23T12:18:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ppalmeida](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/ppalmeida/32/266_2.png) [@ppalmeida](https://discourse.slimframework.com/u/ppalmeida)\
**Post date:** [November 23, 2016, 12:18pm UTC](https://discourse.slimframework.com/t/output-5mb-json-error-slim-slim-slim-http-stream-php-on-line-385/935/1 "2016-11-23T12:18:57Z")

</div>

I have an application with Slim Framework (v3) and there is a method that needs output a (not so) large JSON. It’s about 5Mb.

In my server it goes quite well, but in my local environment it leads to an error:

> Warning: fwrite(): Unable to create temporary file, Check permissions in temporary files directory. slim/slim/Slim/Http/Stream.php on line 385

That file/method writes the content to the http stream. I presume PHP’s memory\_limit was the problem, but no matter I increase the value, the error still there.

The method in that file looks like this:

```
/**
 * Write data to the stream.
 *
 * @param string $string The string that is to be written.
 *
 * @return int Returns the number of bytes written to the stream.
 *
 * @throws RuntimeException on failure.
 */
public function write($string)
{
    //var_dump($string); // -> shows the correct big JSON
    //var_dump($this->isWritable()); // -> evaluates to TRUE
    //die(); 
    if (!$this->isWritable() || ($written = fwrite($this->stream, $string)) === false) {
        throw new RuntimeException('Could not write to stream');
    }

    // reset size so that it will be recalculated on next call to getSize()
    $this->size = null;

    return $written;
}

```

The “var\_dump and die” outputs the correct JSON. The `$this->isWritable()` evaluates to `TRUE`. The problem is at `$written = fwrite($this->stream, $string)`.

Probably it’s related to my local PHP configurations, but I can not figure it out. OR maybe I should use a different approach inside Slim, and not just `$response->withJson($result)`. I saw some posts about Stream a big file, but I am trying avoid write the String to a file and stream the file in the response. Maybe there is better way of doing this.

Any help is much appreciated.

---

<div class="post-metadata">

**Author:** ![llvdl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llvdl/32/78_2.png) [@llvdl](https://discourse.slimframework.com/u/llvdl)\
**Post date:** [November 24, 2016, 8:13pm UTC](https://discourse.slimframework.com/t/output-5mb-json-error-slim-slim-slim-http-stream-php-on-line-385/935/2 "2016-11-24T20:13:10Z")

</div>

This is probably related to the use of `php://temp`, which is used by default by Slim for streams inthe constructor of `Slim\Http\Response`. As stated in the [php docs](http://php.net/manual/en/wrappers.php.php):

> php://memory and php://temp are read-write streams that allow temporary data to be stored in a file-like wrapper. The only difference between the two is that php://memory will always store its data in memory, whereas php://temp will use a temporary file once the amount of data stored hits a predefined limit (the default is 2 MB). The location of this temporary file is determined in the same way as the sys\_get\_temp\_dir() function.

Because of the treshold of 2Mb you won’t run into this problem with most other requests.

To solve this, you can either assign file write permissions to the web server user on the temporary folder or you can use `php://memory` to avoid the creation of the temporary file:

```php
    $fh = fopen('php://memory', 'rw');
    $stream = new Slim\Http\Stream($fh);

    $stream->write(json_encode(['test' => 'test value']));

    return $response->withBody($stream);

```
