3

Assuming I have an array of file handles created using PHP's tmpfile() function, can I send the files as attachments using PHP curl?

Ordinarily, I'd make an associative array in the form $postdata[fieldname] = '@path_to_file', but since I can't determine the path of the tmpfile (I only have the handle), I'm at a loss.

Motivation: I don't want to have to separately purge the contents of these tmpfiles from disk. I feel the tmpfile behavior suits this purpose but the curl component is complicating things.

Post-Answer Consideration: Jay's answer clears this up, but I came across a deal-breaker shortcoming of tmpfile: the "clean-up" behavior is skipped in the event of a FATAL or abrupt termination of the process, so manual purging would be necessary as a fail-safe. I don't think there's any way to circumvent the process of going through the filesystem and ensuring everything is cleaned up, tmpfile or not.

Steve M
  • 335
  • 2
  • 4
  • 12
  • In the documentation of `tmpfile()` you can see two functions allowing you to retrieve the full path : `tempnam()` and sys_get_temp_dir()` – koopajah Feb 06 '13 at 17:18
  • right, but my impression is that only tmpfile cleans itself up at the conclusion of the script or on fclose. – Steve M Feb 06 '13 at 17:20
  • You are right about this assumption yes so I understand why it's easier for you to use `tmpfile()` – koopajah Feb 06 '13 at 17:22

1 Answers1

1

Check out: http://us.php.net/manual/en/function.stream-get-meta-data.php

You can do the following:

$t = tmpfile();
$a = stream_get_meta_data($t);

$filename = $a['uri'];

This question is similar to: Getting filename (or deleting file) using file handle

Community
  • 1
  • 1
Jay
  • 2,123
  • 1
  • 22
  • 29
  • awesome! thank you Jay, and you're right, the question you linked would have saved me this question. – Steve M Feb 06 '13 at 20:48