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

PHP写入文件实现技巧分享

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 11:11:33
Original
2796 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


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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template