php 建立临时目录 临时文件

  • sys_get_temp_dir() 返回临时目录的路径。
<?php
    // 使用 sys_get_temp_dir() 在目录里创建临时文件
    $temp_file = tempnam(sys_get_temp_dir(), 'Tux');
    echo $temp_file;
?>
  • tmpfile() 建立一个临时文件
  • 以读写(w+)模式建立一个具有唯一文件名的临时文件,返回一个文件句柄。
  • 文件会在关闭后(用fclose())自动被删除,或当脚本结束后。
<?php
    $temp = tmpfile();
    fwrite($temp, "writing to tempfile");
    fseek($temp, 0);//文件指针指向头部
    echo fread($temp, 1024);
    fclose($temp); // this removes the file
?>
  • tempnam() 建立一个具有唯一文件名的文件
tempnam ( string $dir , string $prefix )
<?php
    $tmpfname = tempnam("/tmp", "FOO");
    $handle = fopen($tmpfname, "w");
    fwrite($handle, "writing to tempfile");
    fclose($handle);
    unlink($tmpfname);
?>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章