我们知道,在PHP实现读取文件的时候,首先要注意的就是查看这个文件能不能被读取,否则一切都是浪费时间。那么PHP写入文件同样也需要这样做。#t#
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);
?>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
file_put_contents函数在php5中新引进的函数(不知道存在的话用function_exists函数先判断一下)低版本的php无法使用,可以使用如下方式实现PHP写入文件:
$f = fopen($file, 'w');
fwrite($f, $data);
fclose($f);
- 1.
- 2.
- 3.