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?

Martijn
  • 3,696
  • 2
  • 38
  • 64
  • 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. – DaveRandom Jan 17 '12 at 11:58

1 Answers1

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.

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133