PHP文件操作(一)获取文件信息,读取文件

来源:互联网 发布:mac防蹭网软件 编辑:程序博客网 时间:2024/05/18 02:39

一、输入输出流:以内存为参照物。

1、输出流

数据从程序(内存)流向磁盘文件,即:将数据写入到文件中。

2、输入流

数据从磁盘文件流向程序(内存),即:读取文件。

二、常用函数

1、获取文件信息  filesize、filemtime、fileatime、filectime

$filePath = "1.txt";echo "$filePath 大小为:".filesize($filePath)."字节。<br />";date_default_timezone_set("Asia/Shanghai");echo "$filePath 上次修改时间:".date("Y年m月d日 H:i:s",filemtime($filePath))."<br />";//这里注意:默认windows 的NTFS文件系统的访问时间是不启用的,这样会加大文件系统开销。echo "$filePath 上次访问时间:".date("Y年m月d日 H:i:s",fileatime($filePath))."<br />";echo "$filePath 上次文件所有者或文件所在组修改时间:".date("Y年m月d日 H:i:s",filectime($filePath))."<br />";

2、fopen — 打开文件或者 URL 

(1)语法:resource fopen  ( string $filename  , string $mode  [, bool $use_include_path  = false  [, resource $context  ]] )

(2)mode 参数指定了所要求到该流的访问类型,可取值:

'r'  只读方式打开,将文件指针指向文件头。  
'r+'  读写方式打开,将文件指针指向文件头。  
'w'  写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。  
'w+'  读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。  
'a'  写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。  
'a+'  读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。  
'x'  创建并以写入方式打开,将文件指针指向文件头。如果文件已存在,则 fopen()  调用失败并返回 FALSE ,并生成一条 E_WARNING  级别的错误信息。如果文件不存在则尝试创建之。这和给 底层的 open(2) 系统调用指定 O_EXCL|O_CREAT 标记是等价的。  
'x+'  创建并以读写方式打开,其他的行为和 'x' 一样。  
'c'  Open the file for writing only. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to 'w'), nor the call to this function fails (as is the case with 'x'). The file pointer is positioned on the beginning of the file. This may be useful if it's desired to get an advisory lock (see flock() ) before attempting to modify the file, as using 'w' could truncate the file before the lock was obtained (if truncation is desired, ftruncate()  can be used after the lock is requested).  
'c+'  Open the file for reading and writing; otherwise it has the same behavior as 'c'.  

Note:为移植性考虑,强烈建议在用 fopen()打开文件时总是使用'b' 标记。如:$fp = fopen($filePath,"rb");

(3)返回值

成功时返回文件指针资源,如果打开失败,本函数返回 FALSE 。

(4)执行完需要的操作后,必须调用fclose()关闭 

3、fclose — 关闭一个已打开的文件指针 

4、fstat — 通过已打开的文件指针取得文件信息 

$filePath = "1.txt";$fp = fopen($filePath,"rb");if($fp){$fileInfo = fstat($fp);echo "<pre>";//var_dump($fileInfo);echo "</pre>";}else{echo "查看文件失败!";}echo "$filePath 大小为:".$fileInfo["size"]."字节。<br />";date_default_timezone_set("Asia/Shanghai");echo "$filePath 上次修改时间:".date("Y年m月d日 H:i:s",$fileInfo["mtime"])."<br />";//这里注意:默认windows 的NTFS文件系统的访问时间是不启用的,这样会加大文件系统开销。echo "$filePath 上次访问时间:".date("Y年m月d日 H:i:s",$fileInfo["atime"])."<br />";echo "$filePath 上次文件所有者或文件所在组修改时间:".date("Y年m月d日 H:i:s",$fileInfo["ctime"])."<br />";

5、fread — 读取文件(可安全用于二进制文件) 

header("Content-Type:text/html; charset=utf-8");$filePath = "1.txt";if(file_exists($filePath)){$fp = fopen($filePath,"rb");$fileCon = fread($fp,filesize($filePath));//替换\r\n$fileCon = str_replace("\r\n","<br />",$fileCon);echo $fileCon;fclose($fp);}else{echo "文件不存在!请检查。";}

6、feof — 测试文件指针是否到了文件结束的位置

7、大文件的读取方式

header("Content-Type:text/html; charset=utf-8");$filePath = "1.txt";if(file_exists($filePath)){$fileCon = "";$buffer = 30;//一次读取的字节数$fp = fopen($filePath,"rb");while(!feof($fp)){$fileCon .= fread($fp,$buffer);}//替换\r\n$fileCon = str_replace("\r\n","<br />",$fileCon);echo $fileCon;fclose($fp);}else{echo "文件不存在!请检查。";}
8、fgets — 从文件指针中读取一行。可用于大文件读取。

(1)语法:string fgets  ( resource $handle  [, int $length  ] )
(2)可选参数length:从 handle 指向的文件中读取一行并返回长度最多为 length - 1 字节的字符串。碰到换行符(包括在返回值中)、EOF 或者已经读取了 length - 1 字节后停止(看先碰到那一种情况)。如果没有指定 length,则默认为 1K,或者说 1024 字节。 

从 PHP 4.3 开始,忽略掉 length 则行的长度被假定为 1024,将继续从流中读取数据直到行结束。如果文件中的大多数行都大于 8KB,则在脚本中指定最大行的长度在利用资源上更为有效。 
(3)返回值:从指针 handle 指向的文件中读取了 length - 1 字节后返回字符串。 如果文件指针中没有更多的数据了则返回 FALSE 。错误发生时返回 FALSE 。 

header("Content-Type:text/html; charset=utf-8");$filePath = "1.txt";if(file_exists($filePath)){$fileCon = "";$buffer = 1024;//一次读取的字节数$fp = fopen($filePath,"rb");while(!feof($fp)){$fileCon .= fgets($fp,$buffer);}//替换\r\n$fileCon = str_replace("\r\n","<br />",$fileCon);echo $fileCon;fclose($fp);}else{echo "文件不存在!请检查。";}

9、file_get_contents — 将文件读入一个字符串 

string file_get_contents( string $filename  [, bool $use_include_path  = false  [, resource $context  [, int $offset  = -1  [, int $maxlen  ]]]] )

file_get_contents()把文件读入一个字符串。将在参数 offset 所指定的位置开始读取长度为 maxlen 的内容。一个参数时,读取整个文件。如果失败, file_get_contents()将返回 FALSE 。 

$filePath = "1.txt";if(file_exists($filePath)){$fileConAll = file_get_contents($filePath);$fileConPart = file_get_contents($filePath,null,null,0,50);echo changeStr($fileConAll);echo "<br />读取文件中部分内容:<br />".changeStr($fileConPart);}else{echo "文件不存在!请检查。";}//替换\r\nfunction changeStr($str){$str = str_replace("\r\n","<br />",$str);return $str;}

0 0
原创粉丝点击