PHP写入文件实现技巧分享
我们知道,在
PHP写入文件判断是否能被写:
< ?php
- $file = dirlist.php;
- if (is_writable($file)
== false) { - die("我是鸡毛,我不能");
- }
- ?>
能写了的话可以使用file_put_contents函数实现PHP写入文件:
- < ?php
- $file = dirlist.php;
- if (is_writable($file) == false) {
- die(我是鸡毛,我不能);
- }
- $data = 我是可鄙,我想要;
- file_put_contents ($file, $data);
- ?>
file_put_contents函数在php5中新引进的函数(不知道存在的话用function_exists函数先判断一下)低版本的php无法使用,可以使用如下方式实现PHP写入文件:
- $f = fopen($file, w);
- fwrite($f, $data);
- fclose($f);
本文地址:http://www.phprm.com/frame/php1003996.html
转载随意,但请附上文章地址:-)