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
06-Sep-2006 02:53
By the way, this function is really useful for libcurl's CURLOPT_PUT feature if what you're trying to PUT is a string.  For example:

/* Create a cURL handle. */
$ch = curl_init();

/* Prepare the data for HTTP PUT. */
$putString = "Hello, world!";
$putData = tmpfile();
fwrite($putData, $putString);
fseek($putData, 0);

/* Set cURL options. */
curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, $putData);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($putString));
/* ... (other curl options) ... */

/* Execute the PUT and clean up */
$result = curl_exec($ch);
fclose($putData);
curl_close($ch);
03-Aug-2006 09:05
fseek() is important because if you forget about it you will upload empty file...

i had sth like that ^_^
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.