一聚教程网:一个值得你收藏的教程网站

热门教程

php创建临时文件tempnam与tmpfile区别

时间:2022-06-24 23:58:36 编辑:袖梨 来源:一聚教程网

语法
tempnam(dir,prefix)


*/
function dir_wriable($dir)         //自定义函数扩展建立临时文件
{
  $test=tempnam("$dir","test_file");       //建立临时文件
  if($fp=@fopen($test,"w"))        //如果文件成功打开
  {
    @fclose($fp);          //关闭文件
    @unlink($test);          //删除文件
    $wriable="ture";          //返回值为真
  }
  else
  {
    $wriable=false or die("cannot open $test!");    //返回值为假
  }
  return $wriable;          //返回布尔型值
}
if(dir_wriable(str_replace('//','/',dirname(__file__))))    //调用自定义函数
{
  $dir_wriable='建立文件成功';
}
else
{
  $dir_wriable='建立文件失败';
}

/*
如果 php教程 不能在指定的 dir 参数中创建文件,则退回到系统默认值。

注释:本函数的行为在 4.0.3 版中改变了。也会建立一个临时文件以避免竞争情形,即有可能会在产生出作为文件名的字符串与脚本真正建立该文件之间会在文件系统中存在同名文件。注意,如果不再需要该文件则要删除此文件,不会自动删除的。

tmpfile() 函数以读写(w+)模式建立一个具有唯一文件名的临时文件。

文件会在关闭后(用 fclose())自动被删除,或当脚本结束后。


*/

$temp = tmpfile();

fwrite($temp, "testing, testing.");

//倒回文件的开头
rewind($temp);

//从文件中读取 1k
echo fread($temp,1024);

//删除文件
fclose($temp);

热门栏目