Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a file from php://temp wrapper

In PHP, I'm using an in-memory (or rather, temp in-memory) file to load an image from an external URL into a GD resource:

$file = 'php://temp/img';
copy($uri, $file);
$src_img = @imagecreatefromjpeg($file);

However, as I understand it, this file remains in memory, even though I have no use for it after imagecreatefromjpeg().

Is there a way to free memory used by a php://temp wrapper file?

Or atleast signal that the file is no longer used?

like image 771
Martijn Avatar asked Jan 17 '12 11:01

Martijn


1 Answers

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.

like image 126
Jeremy Harris Avatar answered Sep 24 '22 02:09

Jeremy Harris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!