0

Hi I was wondering how to read the contents of a tempname file? I cant seem to access the data added in the example below.

$tmpfname = tempnam("/tmp", "testfile");

$handle = fopen($tmpfname, "w");
fwrite($handle, "I stored data");
echo fread($handle, 1024); / read file?
fclose($handle);  
unlink($tmpfname);

This example works however i'm not able to give the temp file a name which I need to do so I can call it at a later date using CURL.

$temp = tmpfile();
fwrite($temp, "writing to tempfile");
fseek($temp, 0);
echo fread($temp, 1024);
fclose($temp); // this removes the file

Thanks

  • 1
    Don’t think that has anything in particular to do with the fact that this is a temp file … After writing, the pointer position simply is at the end of what you have just written, and you are trying to read _from_ there. You would have to rewind the pointer to the beginning of the file first. – misorude Aug 09 '19 at 12:46
  • 1
    Oh and `w` is write only of course, you need to use `w+` if you want to be able to write _and_ read. – misorude Aug 09 '19 at 12:46

0 Answers0