tmpfile

(PHP 3 >= 3.0.13, PHP 4, PHP 5)

tmpfile -- 建立一个临时文件

说明

resource tmpfile ( void )

以读写 (w+) 模式建立一个具有唯一文件名的临时文件,返回一个与 fopen() 所返回相似的文件句柄。文件会在关闭后(用 fclose())自动被删除,或当脚本结束后。

详细信息请参考系统手册中的 tmpfile(3) 函数,以及 stdio.h 头文件。

例子 1. tmpfile() 例子

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

上例将输出:

writing to tempfile

参见 tempnam()


add a note add a note User Contributed Notes
chris [at] pureformsolutions [dot] com
05-Oct-2005 03:14
I found this function useful when uploading a file through FTP. One of the files I was uploading was input from a textarea on the previous page, so really there was no "file" to upload, this solved the problem nicely:

<?php
  
# Upload setup.inc
  
$fSetup = tmpfile();
  
fwrite($fSetup,$setup);
  
fseek($fSetup,0);
   if (!
ftp_fput($ftp,"inc/setup.inc",$fSetup,FTP_ASCII)) {
       echo
"<br /><i>Setup file NOT inserted</i><br /><br />";
   }
  
fclose($fSetup);
?>

The $setup variable is the contents of the textarea.

And I'm not sure if you need the fseek($temp,0); in there either, just leave it unless you know it doesn't effect it.