php文件操作实例

<?php
function getfileinfo($file)
{
 if(!file_exists($file))
 {
  //如果$file文件不存在,创建它
  touch($file);
  //以写的方式打开刚创建的文件$file
  $fo = fopen($file,"w") or die("Couldn't open $file,sorry");
  //如果文件打开成功:
  if($fo)
  {
   //向文件中写入内容:
   fwrite($fo,"$file did not exist before,it was created by Adam Li on ".date("D d M Y H:i:s",time()));
   //关闭文件
   fclose($fo);
  }  
 }
 //检查是否是文件:
 echo "<p>$file is ".(is_file($file)?"":"not ")."a file</p>";
 //检查是否是目录:
 echo "<p>$file is ".(is_dir($file)?"":"not ")."a directory</p>";
 //检查文件是否可读:
 echo "<p>$file is ".(is_readable($file)?"":"not ")."readable</p>";
 //检查文件是否可写:
 echo "<p>$file is ".(is_writable($file)?"":"not ")."writable</p>";
 //检查文件是否可执行:
 echo "<p>$file is ".(is_executable($file)?"":"not ")."executable</p>";
 //取得文件的上次访问时间
 echo "<p>$file was accessed on ".date("D d M Y H:i:s",fileatime($file))."</p>";
 //取得文件修改时间
 echo "<p>$file was modified on ".date("D d M Y H:i:s",filemtime($file))."</p>";
 //取得文件的 inode 修改时间
 echo "<p>$file was changed on ".date("D d M Y H:i:s",filectime($file))."</p>";

}

$file = "adamli.txt";
getfileinfo($file);
?>

你可能感兴趣的:(php文件操作实例)