PHP create random tmp file and get its full path

前端 未结 2 629
难免孤独
难免孤独 2021-01-01 08:41

After I do:

$temp = tmpfile();
fwrite($temp, \"writing to tempfile\");

I\'d like to get a full path to the $temp file that

相关标签:
2条回答
  • 2021-01-01 09:22
    $path = tempnam(sys_get_temp_dir(), 'prefix');
    

    See this example.

    0 讨论(0)
  • 2021-01-01 09:29

    tmpfile returns a stream-able file pointer.

    To get the corresponding path, ask the stream for its meta data:

    $file = tmpfile();
    $path = stream_get_meta_data($file)['uri']; // eg: /tmp/phpFx0513a
    

    The benefit of the tmpfile approach? PHP automatically removes the $path when $file goes out of scope. With tempnam, you must manually remove the created file.

    0 讨论(0)
提交回复
热议问题