好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php入门教程之文件操作基础 - php文件操作

php入门教程之文件操作基础

在有些场合中需要以文件的形式来对内容进行存储,通常这时候需要对文件进行一系列的操作,PHP中对于文件的操作跟其他计算机程序设计语言对文件的操作类似,对于文件的操作主要包括三个部分,以及围绕这三部分提供的辅助性函数来完成文件操作的工作.

(1)文件的创建与打开;

(2)文件的操作;

(3)文件的关闭;

在PHP中,通过一系列的函数来完成文件的操作,常用的函数及其简要说明罗列如下:

//文件打开,完成文件打开(在文件不存在时可创建文件),依赖于文件中模式的不同而具有不同的操作resource fopen ( string $filename ,string $mode [, bool $use_include_path = false [, resource $context ]] )

//$filename 打开或者要创建文件的路径及文件名,为了便于移植,使用反斜杠/为佳

//$mode 是文件打开的模式,有很多模式,比较常用的r,w,a,同样为了便于移植,建议mode中增加b(二进制)

//通常读文件为 rb ,写文件为 ab,分别表示以二进制读文件及以二进制向文件追加内容

//在文件打开操作过程中出现错误请首先检查文件所在的路径的权限设置

 

实例代码如下:

//文件操作函数     //写操作相关函数   //把$string的内容写到文件指针$handle   int fwrite ( resource  $handle  , string  $string  [, int  $length  ] )    //函数是fwrite的别名函数   fputs ()     //也能完成写操作,不同的是集成了fopen(),fwrite(),fclose(),使用该函数不用再打开关闭文件   int  file_put_contents  ( string  $filename  , mixed  $data  [, int  $flags  = 0 [, resource  $context  ]] )     //读操作相关函数   //逐行读取   string  fgets  ( resource  $handle  [, int  $length  ] )   //逐行读,能够过滤标记   string  fgetss  ( resource  $handle  [, int  $length  [, string  $allowable_tags  ]] )   //逐行读,能够根据定界符输出到数组   array   fgetcsv  ( resource  $handle  [, int  $length  = 0 [, string  $delimiter  =  ','  [, string  $enclosure  =  '"'  [, string  $escape  =  '\'  ]]]] )  //一次性读取一个文件并将文件内容发送到标准输出,包含了文件打开与文件关闭操作   int readfile ( string  $filename  [, bool  $use_include_path  = false [, resource  $context  ]] )  //先要打开文件,然后将文件指针所指向的文件内容发送到标准输出   int  fpassthru  ( resource  $handle  )  //把结果发送到一个数组中   array  file ( string  $filename  [, int  $flags  = 0 [, resource  $context  ]] )  //一次读取一个字符   string  fgetc  ( resource  $handle  )  //读取任意长度字节的内容   string  fread  ( resource  $handle  , int  $length  )    //文件关闭   bool fclose ( resource  $handle  )    //文件操作中常用的函数   //检查文件或目录是否存在   bool  file_exists  ( string  $filename  )  //获取文件大小,返回文件大小的字节数   int  filesize  ( string  $filename  )  //删除文件   bool unlink ( string  $filename  [, resource  $context  ] )  //复位文件指针位置   bool  rewind  ( resource  $handle  )  //在文件指针中定位   int  fseek  ( resource  $handle  , int  $offset  [, int  $whence  = SEEK_SET ] ) 

函数的具体详细的说明在php.net上可以查到,下面练习一下文件的操作,练习名称为『简易日记』,需求如下:

(1)每日记录的内容以年-月-日.txt保存在数据目录下;

(2)首页写日记,并显示以往的记录;

(3)显示单页记录内容;

首页(index.php)实例代码如下:

<!DOCTYPE html>  <html>    <head>      <meta charset= "utf-8" >      <title>简易日记--php文件操作练习</title>      <style>        h1 {           font-size:24px;           border-bottom:1px solid #000;           line-height:40px;        }        .active {           line-height:22px;           font-size:14px;           background:#2cf;           padding:8px;           text-decoration:none;        }        .btn {           width:100px;           height:40px;        }      </style>    </head>    <body>      <h1>我的日记本</h1>      <p><span  class = "active" >写日记</span></p>      <form name= "writediary"  method= "POST"  action= "diaryprocessed.php" >        <p>日记主题</p>        <input type= "text"  name= "diarytopic"  size= "60"  maxlength= "100"  />        <p>日记内容</p>        <textarea name= "diarycontents"  rows= "10"  cols= "53" ></textarea>        <p><input   class = "btn"  type= "submit"  name= "save"  value= "保存"  /></p>      </form>      <hr>      <p><span  class = "active" >日记列表</span> </p>      <hr>      <?php         $handler  = opendir( $_SERVER [ 'DOCUMENT_ROOT' ] .  '/phpcodes/diarydatas/' );         while  (( $diaryname  = readdir( $handler )) !== false) {           if ( $diaryname  !=  "."  &&  $diaryname  !=  ".."  ) {             $files [] =  $diaryname ;          }        }         closedir ( $handler );         foreach ( $files   as   $value ) {           echo   "<a href=viewdiary.php?id="  .  substr ( $value ,0,( strlen ( $value )-4)) .  ">$value</a>"  .  " | " ;        }      ?>    </body>  </html>    保存处理页(diaryprocessed.php)   代码如下 复制代码  <?php  header( 'Content-Type:text/html; charset=utf-8' );    $date  =  gmdate ( 'Y-m-d' , time() + 3600 * 8);    $diarytopic  =  $_POST [ 'diarytopic' ];    $diarycontents  =  $_POST [ 'diarycontents' ];    $DOCUMENT_ROOT  =  $_SERVER [ 'DOCUMENT_ROOT' ];    $output  =  $diarytopic  .  "n"  .  $diarycontents  .  "n" ;    $fp  =  fopen ( $DOCUMENT_ROOT  .  '/phpcodes/diarydatas/'  .  $date  .  '.txt' ,  'ab' );    flock ( $fp ,LOCK_EX);    if (! $fp ) {     echo   "<p><strong>当前不能处理您提交的日志,请稍后再试!</strong></p>" ;     echo   "<a href=" index.htm ">返回</a>" ;  }    fwrite( $fp , $output );    flock ( $fp ,LOCK_UN);    fclose( $fp );    echo   '日记提交成功!' ;  echo   "<a href=" index.htm ">返回</a>" ;    查看内容页(viewdiary.php)   代码如下 复制代码  <?php     $DOCUMENT_ROOT  =  $_SERVER [ 'DOCUMENT_ROOT' ];  ?>  <!DOCTYPE html>  <html>    <head>      <meta charset= "utf-8" >      <title>简易日记</title>      <style>        * { line-height: 32px; }      </style>    </head>    <body>      <a href= "index.php" >写日记</a>      <hr>      <?php         $diaryname  =  $_GET [ 'id' ];         $filepath  =  $DOCUMENT_ROOT  .  '/phpcodes/diarydatas/'  .  $diaryname  .  '.txt' ;         if  ( file_exists ( $filepath )) {           $fp  =  fopen ( $filepath ,  'rb' );           echo   nl2br ( fread ( $fp , filesize ( $filepath )));          fclose( $fp );        }      ?>    </body>  </html> 

查看更多关于php入门教程之文件操作基础 - php文件操作的详细内容...

  阅读:65次