Home > php教程 > php手册 > body text

php与文件操作

WBOY
Release: 2016-06-06 19:55:28
Original
947 people have browsed it

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 if(!flock($fso,LOCK_EX)){//LOCK_NB,排它型锁定 $this-warns('无法锁定缓存文件.');//trigger_error return false; } if(!fwrite($fso,$pagedata)){//写入字节流,serialize写入其他格式 $this-warns

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

 

  if(!flock($fso,LOCK_EX)){//LOCK_NB,排它型锁定

  $this->warns('无法锁定缓存文件.');//trigger_error

  return false;

  }

  if(!fwrite($fso,$pagedata)){//写入字节流,serialize写入其他格式

  $this->warns('无法写入缓存文件.');//trigger_error

  return false;

  }

  flock($fso,LOCK_UN);//释放锁定

  fclose($fso);

  return true;

  }

  ●    复制,删除文件

  php删除文件非常easy,用unlink函数简单操作:

  

  $file = 'dirlist.php';

  $result = @unlink ($file);

  if ($result == false) {

  echo '蚊子赶走了';

  } else {

  echo '无法赶走';

  }

  ?>

  即可.

  复制文件也很容易:

  

  $file = 'yang.txt';

  $newfile = 'ji.txt'; # 这个文件父文件夹必须能写

  if (file_exists($file) == false) {

  die ('小样没上线,无法复制');

  }

  $result = copy($file, $newfile);

  if ($result == false) {

  echo '复制记忆ok';

  }

  ?>

  可以使用rename()函数重命名一个文件夹.其他操作都是这几个函数组合一下就能实现的.

  ●   获取文件属性

  我说几个常见的函数:

  获取最近修改时间:

  

  $file = 'test.txt';

  echo date('r', filemtime($file));

  ?>

  返回的说unix的时间戳,这在缓存技术常用.

  相关的还有获取上次被访问的时间fileatime(),filectime()当文件的权限,所有者,所有组或其它 inode 中的元数据被更新时间,fileowner()函数返回文件所有者

  $owner = posix_getpwuid(fileowner($file));

  (非window系统),ileperms()获取文件的权限,

  

  $file = 'dirlist.php';

  $perms = substr(sprintf('%o', fileperms($file)), -4);

  echo $perms;

  ?>

  filesize()返回文件大小的字节数:

  

  // 输出类似:somefile.txt: 1024 bytes

  $filename = 'somefile.txt';

  echo $filename . ': ' . filesize($filename) . ' bytes';

  ?>

  获取文件的全部信息有个返回数组的函数stat()函数:

  

  $file = 'dirlist.php';

  $perms = stat($file);

  var_dump($perms);

  ?>

  [1] [2] 

php与文件操作

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!