Maison >développement back-end >tutoriel php >PHP写入文件实现技巧分享_PHP教程
我们知道,在
PHP写入文件判断是否能被写:
?php
能写了的话可以使用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>
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>