Thursday 12 September 2019

PHP://temp, php://memory life time, php:// output and how it works with laravel

When you create a file in php://temp (or php://memory for that matter) the resource only lasts the lifetime of the script. If you open the file using fopen() to get a resource handle, the lifetime can be shortened using fclose($resource_handle).
In your case, as soon as your script is done executing, the file will no longer be in memory.
In the circumstance where you would like to clear the memory prior to script completion, all you have to do is fclose() the resource file pointer.
On another note, the /img you are using is invalid and is being ignored. The only added data that is recognized is /maxmemory:n.
https://stackoverflow.com/questions/8894030/how-to-delete-a-file-from-php-temp-wrapper


php://output 
php://output is a write-only stream that allows you to write to the output buffer mechanism in the same way as print and echo.

https://www.php.net/manual/en/wrappers.php.php

These are two of the streams that PHP provides. Streams can be used by functions like fopen, fwrite, stream_get_contents, etc.

php://input is a read-only stream that allows you to read the request body sent to it (like uploaded files or POST variables).

$request_body = stream_get_contents('php://input');
php://output is a writable stream that is sent to Apache and will be returned to the browser that requested your page.

$fp = fopen('php://output', 'w');
fwrite($fp, 'Hello World!'); //User will see Hello World!
fclose($fp);

https://stackoverflow.com/questions/7186189/what-is-the-meaning-of-php-input-php-output-and-when-it-needs-to-use

Laravel you can create a simple cors middleware and set it to global, so it will add cors headers to every response
https://joeyxff.blogspot.com/2019/02/create-project-middleware.html

Then in your controller you can just use:

$fp = fopen('php://output', 'w');
fwrite($fp, 'Hello World!'); //User will see Hello World!
fclose($fp);

Without any return response statement

No comments:

Post a Comment