How to delete a file from php://temp wrapper
Asked Answered
E

1

11

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?

Eudemon answered 17/1, 2012 at 11:44 Comment(1)
Not sure this is possible - I hope it is, seems pretty ridiculous if it isn't. However, a potential work around would be to use php://maxmemory to set a very low memory value so the data would always be written to disk, keeping memory usage low. Although if your going to do that you might as well just use a temp file, rendering the whole php://temp thing pointless.Wondering
M
8

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.

Minardi answered 18/1, 2012 at 16:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.