This question already has an answer here:
这个问题已经有了答案:
- How to get a temporary file path? 5 answers
- 如何获得临时文件路径?5个回答
I tried this:
我试着这样的:
$temp = tmpfile();
file_put_contents($temp,file_get_contents("$path/$filename"));
But I get this error: "Warning: file_put_contents() expects parameter 1 to be string,"
但是我得到了这个错误:“警告:file_put_contents()期望参数1为string,”
If I try:
如果我尝试:
echo file_get_contents("$path/$filename");
It return to screen the file content as a long string. Where am I wrong?
它返回将文件内容作为一个长字符串显示。我在哪儿错了?
2 个解决方案
#1
9
tmpfile()
creates a temporary file with a unique name in read-write (w+) mode and returns a file handle to use with fwrite for example.
tmpfile()以读-写(w+)模式创建一个具有唯一名称的临时文件,并返回一个文件句柄,以供fwrite使用。
$temp = tmpfile();
fwrite($temp, file_get_contents("$path/$filename"));
The file is automatically removed when closed (for example, by calling fclose(), or when there are no remaining references to the file handle returned by tmpfile()), or when the script ends. look at php ref.
当文件被关闭时(例如,通过调用fclose(),或者当tmpfile()返回的文件句柄没有其他引用时,或者脚本结束时),文件将被自动删除。查看php ref。
#2
15
In the example you give you want tempnam()
and not tmpfile()
.
在示例中,您希望使用tempnam()而不是tmpfile()。
-
tempnam()
creates a temporary file and returns the path to it as a String. You can then pass that string into file_put_contents. You must remember to manually delete the temporary file once you are done with it.tempnam()创建一个临时文件,并以字符串的形式返回路径。然后可以将该字符串传递到file_put_contents。您必须记住,在使用临时文件之后,要手动删除它。
-
tmpfile()
creates a temporary file and returns a file resource/pointer to use withfwrite()
and other file manipulation functions. In addition, once the script execution ends, the temporary file created bytmpfile()
is automatically deleted.tmpfile()创建一个临时文件,并返回一个与fwrite()和其他文件操作函数一起使用的文件资源/指针。此外,脚本执行结束后,tmpfile()创建的临时文件将自动删除。
Here is your example script using tempnam()
instead of tmpfile()
:
下面是使用tempnam()代替tmpfile()的示例脚本:
$temp = tempnam(sys_get_temp_dir(), 'TMP_');
file_put_contents($temp, file_get_contents("$path/$filename"));
#1
9
tmpfile()
creates a temporary file with a unique name in read-write (w+) mode and returns a file handle to use with fwrite for example.
tmpfile()以读-写(w+)模式创建一个具有唯一名称的临时文件,并返回一个文件句柄,以供fwrite使用。
$temp = tmpfile();
fwrite($temp, file_get_contents("$path/$filename"));
The file is automatically removed when closed (for example, by calling fclose(), or when there are no remaining references to the file handle returned by tmpfile()), or when the script ends. look at php ref.
当文件被关闭时(例如,通过调用fclose(),或者当tmpfile()返回的文件句柄没有其他引用时,或者脚本结束时),文件将被自动删除。查看php ref。
#2
15
In the example you give you want tempnam()
and not tmpfile()
.
在示例中,您希望使用tempnam()而不是tmpfile()。
-
tempnam()
creates a temporary file and returns the path to it as a String. You can then pass that string into file_put_contents. You must remember to manually delete the temporary file once you are done with it.tempnam()创建一个临时文件,并以字符串的形式返回路径。然后可以将该字符串传递到file_put_contents。您必须记住,在使用临时文件之后,要手动删除它。
-
tmpfile()
creates a temporary file and returns a file resource/pointer to use withfwrite()
and other file manipulation functions. In addition, once the script execution ends, the temporary file created bytmpfile()
is automatically deleted.tmpfile()创建一个临时文件,并返回一个与fwrite()和其他文件操作函数一起使用的文件资源/指针。此外,脚本执行结束后,tmpfile()创建的临时文件将自动删除。
Here is your example script using tempnam()
instead of tmpfile()
:
下面是使用tempnam()代替tmpfile()的示例脚本:
$temp = tempnam(sys_get_temp_dir(), 'TMP_');
file_put_contents($temp, file_get_contents("$path/$filename"));