为了避免使用磁盘上的实际文件进行写 – 读 – 删除循环,我会使用php的内置php:// temp和php:// memory
IO stream wrappersdocs将所有临时“文件”数据保存在内存中.
// open a temporary file handle in memory
$tmp_handle = fopen('php://temp', 'r+');
fwrite($tmp_handle, 'my awesome text to be emailed');
// do some more stuff, then when you want the contents of your "file"
rewind($tmp_handle);
$file_contents = stream_get_contents($tmp_handle);
// clean up your temporary storage handle
fclose($tmp_handle);
您永远不必将文件写入或删除到磁盘.另外,请注意使用php:// temp和php://来自主题文档的内存之间的区别:
php://memory and php://temp are read-write streams that allow temporary data to be stored in a file-like wrapper. The only difference between the two is that php://memory will always store its data in memory, whereas php://temp will use a temporary file once the amount of data stored hits a predefined limit (the default is 2 MB). The location of this temporary file is determined in the same way as the sys_get_temp_dir() function.