PHP 文件操作
-
读取文件内容
<?php $filename = '文件路径'; //通过函数 file_get_contents()函数度读取文件内容 //$file = file_get_contents($filename); //echo $file; //通过打开文件的形式进行读取文件内容 参数中的"b"的作用是在操作二进制文件时如果没有指定 "b" 标记,可能会碰到一些奇怪的问题,包括坏掉的图片文件以及关于 字符的奇怪问题。 $file = fopen($filename,"rb"); $content = ''; while(!feof($file)){ $content .= fread($file,10);//每次读取文件十个字符 } echo $content; //输出读取的内容 fclose($file); //关闭文件
-
判断文件是否存在
<?php $filename = '文件路径'; //判断如果$filename文件存在的话 就输出文件内容 //is_file if(is_file($filename)){ echo '文件存在'; } //file_exists if(file_exists($filename)){ echo '文件存在'; } //is_readable //如果由 filename 指定的文件或目录存在并且可读则返回 TRUE,否则返回 FALSE。 if(is_readable($filename)){ echo '文件可读'; } //is_writeable 是is_writable 的别名 // 返回值 如果文件 filename 存在并且可写则返回 TRUE。 if(!is_writable($filename)){ echo '文件不可写'; }