Home  >  Article  >  Backend Development  >  How to use related functions to read files in PHP_PHP tutorial

How to use related functions to read files in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:34:25972browse

in

  1. < ?php
  2. $file = 'dirlist.php';
  3. if (is_readable($file)
    == false) {
  4. die('File does not exist or cannot be read');
  5. } else {
  6. echo 'exists';
  7. }
  8. ?>

The function to determine the existence of a file also includes file_exists (demonstrated below), but this is obviously not as comprehensive as is_readable. When a file If it exists, you can use

<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">"filelist.php"</span><span>;  </span></li><li class="alt"><span>if (file_exists($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">file_get_contents</span><span>($file);  </span></li><li class="alt"><span>echo htmlentities($data);  </span></li><li><span class="tag">?></span><span> </span></span></li>
<li class="alt"><span> </span></li>
</ol>

. However, the file_get_contents function is not supported in lower versions. You can first create a handle to the file, and then use the pointer to read all :

$fso = fopen($cacheFile, 'r');
$data = fread($fso, filesize($cacheFile));
fclose($fso);

There is another way to read binary files:

$data = implode('', file($file));


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445974.htmlTechArticleat?php $ file = 'dirlist.php' ; if(is_readable($file) ==false){ die('The file does not exist or cannot be read'); }else{ echo 'exists'; }? The function to determine the existence of a file is also file_exists (below...
Statement:
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