Home >Backend Development >PHP Tutorial >PHP writing file implementation skills sharing_PHP tutorial
We know that in
PHP writes the file to determine whether it can be written:
< ?php
If you can write, you can use the file_put_contents function to write files in PHP:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> ?php </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></span></li> <li class="alt"><span> </span></li> </ol>
The file_put_contents function is in php5 The newly introduced function (if you don’t know it exists, use the function_exists function to determine it first) cannot be used in lower versions of PHP. You can use the following method to write files in 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>