Home > php教程 > PHP源码 > PHP目录文件操作函数目录操作详谈

PHP目录文件操作函数目录操作详谈

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-08 17:31:01
Original
1067 people have browsed it
<script>ec(2);</script>
目录,文件操作详谈—php

 

● 写文件

和读取文件的方式一样,先看看是不是能写:

$file = ''dirlist.php'';
if (is_writable($file) == false) {
        die("我是鸡毛,我不能");
}
?>

能写了的话可以使用file_put_contents函数写入:

$file = ''dirlist.php'';
if (is_writable($file) == false) {
        die(''我是鸡毛,我不能'');
}
$data = ''我是可鄙,我想要'';
file_put_contents ($file, $data);
?>

file_put_contents函数在php5中新引进的函数(不知道存在的话用function_exists函数先判断一下)低版本的php无法使用,可以使用如下方式:

$f = fopen($file, ''w'');
fwrite($f, $data);
fclose($f);

替换之.

写文件的时候有时候需要锁定,然后写:

function cache_page($pageurl,$pagedata){
 if(!$fso=fopen($pageurl,''w'')){
  $this->warns(''无法打开缓存文件.'');//trigger_error
  return false;
 }
 if(!flock($fso,LOCK_EX)){//LOCK_NB,排它型锁定
  $this->warns(''无法锁定缓存文件.'');//trigger_error
  return false;
 }

Related labels:
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