好得很程序员自学网

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

php 文件读取写操作代码(fopen,is_writable,fwrite) - p

php 文件读取写操作代码(fopen,is_writable,fwrite)

php文件操作,包括对文件函数fopen,is_writable,fwrite,fclose来进行文件操作,下面先看实例.

<?php   $filename  =  "html/cache.txt" ;   $contents  =  "我是张斌" ;   if ( is_writable ( $filename )){   if (( $handle  =  fopen ( $filename , "a" ) )== false){   echo   "写入文件 $filename 失败" ;   exit ();   }   if (fwrite( $handle , $contents ) == false){   echo   "写入文件$filename失败" ;   exit ();   }   echo   "写入文件 $filename 成功" ;    fclose( $handle );   } else {   echo   "文件$filename不可写入" ;   }   ?> 

fopen() 函数打开文件或者 URL。

如果打开失败,本函数返回 FALSE。

语法

fopen(filename,mode,include_path,context)

参数 描述

filename 必需。规定要打开的文件或 URL。

mode 必需。规定要求到该文件/流的访问类型。可能的值见下表。

include_path 可选。如果也需要在 include_path 中检索文件的话,可以将该参数设为 1 或 TRUE。

context 可选。规定文件句柄的环境。Context 是可以修改流的行为的一套选项。

定义和用法

fwrite() 函数写入文件(可安全用于二进制文件)。

语法

fwrite(file,string,length)

参数 描述

file 必需。规定要写入的打开文件。

string 必需。规定要写入文件的字符串。

length 可选。规定要写入的最大字节数。

说明

fwrite() 把 string 的内容写入文件指针 file 处。 如果指定了 length,当写入了 length 个字节或者写完了 string 以后,写入就会停止,视乎先碰到哪种情况。

fwrite() 返回写入的字符数,出现错误时则返回 false。

<?php  $file  =  fopen ( "test.txt" , "w" );  echo  fwrite( $file , "Hello World. Testing!" );  fclose( $file );  ?> 

is_writable (PHP 4中,PHP 5中),is_writable -告诉是否filename是写.

描述

布尔is_writable(字符串$文件名)

返回TRUE,如果文件存在且可写,该文件名参数可能是一个目录名称,让您可以检查,如果是可写的目录.

请记住,PHP的可访问该文件的用户ID的Web服务器运行(通常是'谁'),安全模式的限制是没有考虑到.

<?php  $filename  =  'test.txt' ;  if  ( is_writable ( $filename )) {       echo   'The file is writable' ;  }  else  {       echo   'The file is not writable' ;  }  ?> 

查看更多关于php 文件读取写操作代码(fopen,is_writable,fwrite) - p的详细内容...

  阅读:64次