Home > php教程 > php手册 > 如何运用相关函数实现PHP读取文件

如何运用相关函数实现PHP读取文件

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 11:11:35
Original
1193 people have browsed it

  1.  ?php  
  2. $file = 'dirlist.php';  
  3. if (is_readable($file)
     == false) {  
  4. die('文件不存在或者无法读取');  
  5. } else {  
  6. echo '存在';  
  7. }  
  8. ?> 
  9.  

判断文件存在的函数还有file_exists(下面演示),但是这个显然无is_readable全面.,当一个文件存在的话可以用

<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">"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>
</li>
<li class="alt"><span> </span></li>
</ol>
Copy after login

但是file_get_contents函数在较低版本上不支持,可以先创建文件的一个句柄,然后用指针读取全部:

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

还有一种方式,可以读取二进制的文件:

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


Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template