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.