php读取本地文件常用方法

 更新时间:2013年9月26日 08:07  点击:774

下面我们以.txt文件为实例来介绍php读取本地文件的函数,读取文件我们可以利用fopen或file_get_contents来读取,file_get_contents更简单而fopen需要fread配合才可以显示读出的内容。

1.首先来介绍一下fopen()函数

下面我们给出了一个直接打开本地文件的代码例子,必要的地方我们都已经加上了注释:

代码如下: 

 <?php   
    //假若我们本地的文件是一个名为readme.txt的文本   
    $filedemo = "readme.txt";   
    $fpdemo = fopen($filedemo,"r");   
    if ($fpdemo){   
     while(!feof($fpdemo)){   
      //1000读取的字符数   
      $datademo = fread($fpdemo, 1000);   
     }   
     fclose($fpdemo);   
    }   
    echo $datademo;   
    ?>

2.使用file_get_contents() 函数将整个文件在一个字符串中输出:

假若我们的xmlas.txt文本文件中有这样一句话:现在的电影越来越没激情,想要全部高潮请看日本爱情动作片!

那么我们file_get_contents() 函数的实例代码便如下:

代码如下:

<?php   
    echo file_get_contents("readme.txt");   
    //此时输出的内容便为readme.txt中的内容:   
    //现在的电影越来越没激情,想要全部高潮请看日本爱情动作片!   
    ?>

3.如何读取本地的一个文件夹而不是一个单独文件:
请看下面的实例代码,在实例中我们将读取一个名为xmlas的文件夹:

代码如下:

<?php   
    $dirdemo = opendir('/xmlas');   
    while(($filedemo = readdir($dirdemo))!=false){   
      if ($filedemo!="." && $filedemo!="..") {    
        $nsdemo = explode('.', $filedemo);   
        echo $nsdemo[0];   
      }    
    }   
    closedir($dirdemo);   
    ?>

 

4.我们还可以利用fopen来写文件

代码如下:

/**
 *用fopen写入文件
 *@param string $filename
 *@param string $contents
 *@return boolean
 */

 function wirte($filename,&$contents)
 {
  $fp=fopen($filename,"wb");
  if($fp)
  {
   flock($fp,LOCK_EX);//同一时间锁定文件,只能一个人操作
   fwrite($fp,$contents);
   flock($fp,LOCK_UN);//保存数据握进行解锁文件并保存
   fclose($fp);
   return true;
  }else
  {
   return false;
  }
 }

这样我只要利用fopen配合fwrite就可以实现文件读写了。

注意:l打开文件

在打开文件文件之前,我们需要知道这个文件的路径,以及此文件是否存在。

用$_SERVER[“DOCUMENT_ROOT”]内置全局变量,来获得站点的相对路径。如下:

代码如下:

$root = $_SERVER[“DOCUMENT_ROOT”];



在用函数file_exists()来检测文件是否存在。如下:

代码如下:

If(!file_exists("$root/order.txt")){echo ‘文件不存在';}

这样可能更合理更实用哦。

[!--infotagslink--]

相关文章

  • php 中file_get_contents超时问题的解决方法

    file_get_contents超时我知道最多的原因就是你机器访问远程机器过慢,导致php脚本超时了,但也有其它很多原因,下面我来总结file_get_contents超时问题的解决方法总结。...2016-11-25
  • php file_get_contents 设置代理抓取页面示例

    file_get_contents函数在php中可以直接打开本地文件也可以直接抓取远程服务器文件,如果简单的采集我们可以使用file_get_contents直接来操作,如果有防采集我们可能需要...2016-11-25
  • php报错file_get_contents(): php_network_getaddresses问题

    本文章来为各位介绍一篇关于file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known...错误解决办法。 昨天,服务器的DN...2016-11-25
  • PHP file_get_contents设置超时处理方法

    file_get_contents的超时处理话说,从PHP5开始,file_get_content已经支持context了(手册上写着:5.0.0 Added the context support. ),也就是说,从5.0开始,file_get_contents其实也可以POST数据。今天说的这篇是讲超时的,确实在...2013-10-04
  • file_get_contents()获取https出现这个错误Unable to find the wrapper “https”

    下面我们来看一篇关于file_get_contents()获取https出现这个错误Unable to find the wrapper “https”问题的解决办法. file_get_contents()获取https出现这个错...2016-11-25
  • php提示Warning: file_get_contents(): couldn’t resolve

    在使用file_get_contents函数获取远程文件时提示Warning: file_get_contents(): couldn’t resolve错误了,这个我们可以看出是dns的问题,解决办法也简单。 今天在...2016-11-25
  • file_get_contents不能获取带端口的网址

    本文章来给各位同学介绍file_get_contents不能获取带端口的网址解决办法,有需要了解的同学可参考。 先们来了解file_get_contents() 函数,官方介绍说它是把整个...2016-11-25
  • php中file_get_contents和curl_get_contents介绍

    php中file_get_contents和curl_get_contents介绍 有需要的朋友可参考一下。 分享一个实际在用的函数: file_get_contents() 函数是用于将文件的内容读入到一个字符...2016-11-25
  • php curl file_get_contents post方式获取数据

    下面我们在这里来为各位介绍一篇关于php curl file_get_contents post方式获取数据例子,希望文章能够帮助到各位朋友. curl post,file_get_contents post,curl fi...2016-11-25
  • 解决C++ fopen按行读取文件及所读取的数据问题

    今天小编就为大家分享一篇解决C++ fopen按行读取文件及所读取的数据问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-25
  • php file_get_contents 函数

    php file_get_contents 函数 file_get_contents ( PHP 4中“ = 4.3.0 , PHP 5中) file_get_contents -读取整个文件转换成字符串 描述 字符串file...2016-11-25
  • php file_get_contents获取百度热词代码

    这是一段很简单的程序利用了php的file_get_contents函数来采集百度的数据,然后通过simplexml_load_String把它数据解析出来,这样数据就保存到了一个数组,我们就可以方便...2016-11-25
  • file file_get_contents HTTP request failed

    /* 我有一个问题,要求从php教程代码的url。我需要调用一个服务,使用从我的php代码的查询字符串。如果我的浏览器中键入一个网址,它工作还算可以,但如果我使用文件获取,内容...2016-11-25
  • file_get_contents实现数据Post数据方法

    file_get_contents() 函数把整个文件读入一个字符串中。 和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。 file_get_contents() 函数是用于将文件...2016-11-25
  • php中file_get_contents函数高级用法

    file_get_contents函数我们通常是拿来对文件操作了,下面一起来看看file_get_contents函数的高级使用方法吧. 首先解决file_get_contents的超时问题,在超时返回错误...2016-11-25
  • php file_get_contents与curl()函数对比

    在php中file_get_contents与curl()函数都可以用来抓取对方网站的数据并保存到本地服务器中,但是总得来讲file_get_contents()效率稍低些,常用失败的情况、curl()效率挺...2016-11-25
  • PHP Warning: file_get_contents failed to open stream解决办法

    file_get_contents函数在获得远程文件时提示Warning: file_get_contents failed to open stream,希望例子能够帮助到各位,希望例子能够帮助到大家。 在做项目时用 f...2016-11-25
  • php file_get_contents函数轻松采集ip138数据

    <?php教程 //全国,判断条件是$REQUEST_URI是否含有html if (!strpos($_SERVER["REQUEST_URI"],".html")) { $page="http://qq.ip138.com/weather/"; $html =...2016-11-25
  • file_get_contents() 函数把整个文件读入一个字符串中

    语法 file_get_contents(path,include_path,context,start,max_length) path 必需。规定要读取的文件。 include_path 可选。如果也想在 include_pat...2016-11-25
  • file_get_contents被屏蔽解决方法

    在php中file_get_contents函数可直接采集远程服务器内容,然后保存到一个变量中了,介理一般都会把file_get_contents、fsockopen等一些IO操作的函数禁用掉,因为它们怕被 D...2016-11-25