当前位置:Gxlcms > PHP教程 > 如何运用相关函数实现PHP读取文件_PHP教程

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

时间:2021-07-01 10:21:17 帮助过:2人阅读

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

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

  1. < ?php
  2. $file = "filelist.php";
  3. if (file_exists($file) == false) {
  4. die('文件不存在');
  5. }
  6. $data = file_get_contents($file);
  7. echo htmlentities($data);
  8. ?>

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

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

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

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


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445974.htmlTechArticle在 ?php $ file = 'dirlist.php' ; if(is_readable($file) ==false){ die('文件不存在或者无法读取'); }else{ echo'存在'; } ? 判断文件存在的函数还有file_exists(下面...

人气教程排行