title: Stream from a temporary file, then delete when finished?
body: <p>I'm writing a temporary file by running a couple of external Unix tools over a PDF file (basically I'm using QPDF and sed to alter the colour values. Don't ask.):</p>
  
  <pre><code>// Uncompress PDF using QPDF (doesn't read from stdin, so needs tempfile.)
  $compressed_file_path = tempnam(sys_get_temp_dir(), 'cruciverbal');
  file_put_contents($compressed_file_path, $response-&gt;getBody());  
  $uncompressed_file_path = tempnam(sys_get_temp_dir(), 'cruciverbal');
  $command = "qpdf --qdf --object-streams=disable '$compressed_file_path' '$uncompressed_file_path'";
  exec($command, $output, $return_value);
  
  // Run through sed (could do this bit with streaming stdin/stdout)
  $fixed_file_path = tempnam(sys_get_temp_dir(), 'cruciverbal');
  $command = "sed s/0.298039215/0.0/g &lt; '$uncompressed_file_path' &gt; '$fixed_file_path'";
  exec($command, $output, $return_value);
  </code></pre>
  
  <p>So, when this is done I'm left with a temporary file on disk at <code>$fixed_file_path</code>. (NB: While I could do the whole <code>sed</code> process streamed in-memory without a tempfile, the QPDF utility <a href="https://github.com/qpdf/qpdf/issues/54" rel="nofollow noreferrer">requires an actual file as input</a>, for good reasons.)</p>
  
  <p>In my existing process, I then read the whole <code>$fixed_file_path</code> file in as a string, delete it, and hand the string off to another class to go do things with.</p>
  
  <p>I'd now like to change that last part to using a PSR-7 stream, i.e. a <code>\Guzzle\Psr7\Stream</code> object. I figure it'll be more memory-efficient (I might have a few of these in the air at once) and it'll need to be a stream in the end.</p>
  
  <p>However, I'm not sure then how I'd delete the temporary file when the (third-party) class I'd handed the stream off to is finished with it. Is there a method of saying "...and delete that when you're finished with it"? Or auto-cleaning my temporary files in some other way, without keeping track of them manually? </p>
  
  <p>I'd been vaguely considering rolling my own <code>SelfDestructingFileStream</code>, but that seemed like overkill and I thought I might be missing something.</p>