Home > php教程 > php手册 > PHP写入文件实现技巧分享

PHP写入文件实现技巧分享

WBOY
Release: 2016-06-13 11:11:33
Original
2777 people have browsed it

我们知道,在

PHP写入文件判断是否能被写:

 ?php  

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

能写了的话可以使用file_put_contents函数实现PHP写入文件:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php  </span></span></span></li>
<li>
<span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">'dirlist.php'</span><span>;  </span>
</li>
<li class="alt"><span>if (is_writable($file) == false) {  </span></li>
<li><span>die('我是鸡毛,我不能');  </span></li>
<li class="alt"><span>}  </span></li>
<li>
<span>$</span><span class="attribute">data</span><span> = </span><span class="attribute-value">'我是可鄙,我想要'</span><span>;  </span>
</li>
<li class="alt"><span>file_put_contents ($file, $data);  </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
<li class="alt"><span> </span></li>
</ol>
Copy after login

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

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">f</span><span> = </span><span class="attribute-value">fopen</span><span>($file, 'w');  </span></span></li>
<li><span>fwrite($f, $data);  </span></li>
<li class="alt"><span>fclose($f);  </span></li>
</ol>
Copy after login


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