kik*_*s73 9 php temporary-files
我试过这个:
$temp = tmpfile();
file_put_contents($temp,file_get_contents("$path/$filename"));
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:"警告:file_put_contents()期望参数1为字符串,"
如果我尝试:
echo file_get_contents("$path/$filename");
Run Code Online (Sandbox Code Playgroud)
它返回以将文件内容筛选为长字符串.我哪里错了?
Aer*_*roX 27
在这个例子中,你给了你想要tempnam()而不是tmpfile().
tempnam()创建一个临时文件并以String形式返回它的路径.然后,您可以将该字符串传递给file_put_contents.完成后,您必须记住手动删除临时文件.
tmpfile()创建一个临时文件并返回一个文件资源/指针以与fwrite()其他文件操作函数一起使用.此外,一旦脚本执行结束,将tmpfile()自动删除创建的临时文件.
这是您的示例脚本,tempnam()而不是tmpfile():
$temp = tempnam(sys_get_temp_dir(), 'TMP_');
file_put_contents($temp, file_get_contents("$path/$filename"));
Run Code Online (Sandbox Code Playgroud)
小智 16
tmpfile() 在读写(w +)模式下创建一个具有唯一名称的临时文件,并返回一个文件句柄以用于fwrite.
$temp = tmpfile();
fwrite($temp, file_get_contents("$path/$filename"));
Run Code Online (Sandbox Code Playgroud)
关闭时会自动删除该文件(例如,通过调用fclose(),或者对tmpfile()返回的文件句柄没有剩余引用),或者脚本结束时.看看php ref.