PHP常用的文件操作函数

来源:互联网 发布:linux rpm qa grep 编辑:程序博客网 时间:2024/05/21 13:57
一 、解析路径:

1 获得文件名:basename();

2 得到目录部分:dirname();

3 得到路径关联数组:pathinfo();

二、文件类型:

1. filetype();


三、得到给定文件有用信息数组(很有用)

1. fstat();通过已打开的文件指针取得文件信息

2. stat()获取由 filename 指定的文件的统计信息(类比fstat())


四、计算大小

1. filesize()

2. disk_free_space()获得目录所在磁盘分区的可用空间(字节单位)

3. disk_total_space()返回一个目录的磁盘总大小


五、 访问与修改时间

1. fileatime(): 最后访问时间

2. filectime(): 最后改变时间(任何数据的修改)

3. filemtime(): 最后修改时间(指仅是内容修改)


六、 文件的I/O操作

1. fopen -- 打开文件或者 URL

2. file -- 把整个文件读入一个数组中(此函数是很有用的)

3. fgets -- 从文件指针中读取一行

4. fgetss -- 从文件指针中读取一行并过滤掉 HTML 标记(和 fgets() 相同,只除了 fgetss 尝试从读取的文本中去掉任何 HTML 和 PHP 标记)。


七、对目录的操作:

1. opendir -- 打开目录句柄,打开一个目录句柄,可用于之后的 closedir(),readdir() 和 rewinddir() 调用中。

2. readdir -- 从目录句柄中读取条目,返回目录中下一个文件的文件名。文件名以在文件系统中的排序返回。

3. scandir -- 列出指定路径中的文件和目录(很有用),返回一个 array,包含有 directory 中的文件和目录。


八、 对文件属性的操作(操作系统环境不同,可能有所不一样,这点要注意)

1、文件是否可读:boolis_readable ( string filename )

2、文件是否可写:bool is_writable ( string filename )

3、检查文件是否存在 boolfile_exists ( string filename )


九、可直接使用在项目中的超强PHP文件操作类,

本帖隐藏的内容

PHP文件操作类
  1. <?php  
  2. /*************************************************************************************** 
  3. 文件名:File.cls.php 
  4. 文件简介:类clsFile的定义,对文件操作的封装 
  5. ****************************************************************************************/  
  6. !defined('INIT_PHPV') && die('No direct script access allowed');  
  7. class clsFile  
  8. {  
  9.    private $fileName_str;         //文件的路径  
  10.    private $fileOpenMethod_str;   //文件打开模式  
  11.      
  12.    function __construct($fileName_str='',$fileOpenMethod_str='readOnly')//路径,默认为空;模式,默认均为只读  
  13.    {  
  14.        //构造函数,完成数据成员的初始化  
  15.        $this->fileName_str=$fileName_str;  
  16.        $this->fileOpenMethod_str=$fileOpenMethod_str;  
  17.    }  
  18.      
  19.    function __destruct()  
  20.    {  
  21.        //析构函数  
  22.    }  
  23.      
  24.    public function __get($valName_val)//欲取得的数据成员名称  
  25.    {  
  26.        //特殊函数,取得指定名称数据成员的值  
  27.           return $this->$valName_val;  
  28.    }  
  29.      
  30.    private function on_error($errMsg_str='Unkown Error!',$errNo_int=0)//错误信息,错误代码  
  31.    {  
  32.         echo '程序错误:'.$errMsg_str.'错误代码:'.$errNo_int;//出错处理函数  
  33.    }  
  34.      
  35.    public function open()  
  36.    {  
  37.        //打开相应文件,返回文件资源标识  
  38.           //根据fileOpenMethod_str选择打开方式  
  39.           switch($this->fileOpenMethod_str)  
  40.           {  
  41.                  case 'readOnly':  
  42.                     $openMethod_str='r';      //只读,指针指向文件头  
  43.                     break;  
  44.                  case 'readWrite':  
  45.                     $openMethod_str='r+';     //读写,指针指向文件头  
  46.                     break;  
  47.                  case 'writeAndInit':  
  48.                     $openMethod_str='w';      //只写,指针指向文件头将大小截为零,不存在则创建  
  49.                     break;  
  50.                  case 'readWriteAndInit':  
  51.                     $openMethod_str='r+';     //读写,指针指向文件头将大小截为零,不存在则创建  
  52.                     break;  
  53.                  case 'writeAndAdd':  
  54.                     $openMethod_str='a';      //只写,指针指向文件末尾,不存在则创建  
  55.                     break;  
  56.                  case 'readWriteAndAdd':  
  57.                     $openMethod_str='a+';     //读写,指针指向文件末尾,不存在则创建  
  58.                     break;  
  59.                  default:  
  60.                     $this->on_error('Open method error!',310);//出错处理  
  61.                     exit;  
  62.           }  
  63.             
  64.           //打开文件         
  65.           if(!$fp_res=fopen($this->fileName_str,$openMethod_str))  
  66.           {  
  67.                  $this->on_error('Can\'t open the file!',301);//出错处理  
  68.                  exit;  
  69.           }  
  70.             
  71.           return $fp_res;  
  72.    }  
  73.      
  74.    public function close($fp_res)//由open返回的资源标识  
  75.    {  
  76.        //关闭所打开的文件  
  77.           if(!fclose($fp_res))  
  78.           {  
  79.                  $this->on_error('Can\'t close the file!',302);//出错处理  
  80.                  exit;  
  81.           }  
  82.    }  
  83.      
  84.    public function write()//$fp_res,$data_str,$length_int:文件资源标识,写入的字符串,长度控制  
  85.    {  
  86.        //将字符串string_str写入文件fp_res,可控制写入的长度length_int  
  87.           //判断参数数量,调用相关函数  
  88.           $argNum_int=func_num_args();//参数个数  
  89.             
  90.           $fp_res=func_get_arg(0);          //文件资源标识  
  91.           $data_str=func_get_arg(1);        //写入的字符串  
  92.             
  93.           if($argNum_int==3)  
  94.           {  
  95.                  $length_int=func_get_arg(2);  //长度控制  
  96.               if(!fwrite($fp_res,$data_str,$length_int))  
  97.               {  
  98.                     $this->on_error('Can\'t write the file!',303);//出错处理  
  99.                     exit;  
  100.               }  
  101.           }  
  102.           else  
  103.           {  
  104.                  if(!fwrite($fp_res,$data_str))  
  105.               {  
  106.                     $this->on_error('Can\'t write the file!',303);//出错处理  
  107.                     exit;  
  108.               }  
  109.           }  
  110.    }  
  111.      
  112.    public function read_line()//$fp_res,$length_int:文件资源标识,读入长度  
  113.    {  
  114.        //从文件fp_res中读入一行字符串,可控制长度  
  115.           //判断参数数量  
  116.           $argNum_int=func_num_args();  
  117.           $fp_res=func_get_arg(0);  
  118.             
  119.           if($argNum_int==2)  
  120.           {  
  121.               $length_int=func_get_arg(1);  
  122.               if($string_str=!fgets($fp_res,$length_int))  
  123.               {  
  124.                     $this->on_error('Can\'t read the file!',304);//出错处理  
  125.                     exit;  
  126.               }  
  127.               return $string_str;  
  128.        }  
  129.        else  
  130.        {  
  131.               if(!$string_str=fgets($fp_res))  
  132.               {  
  133.                     $this->on_error('Can\'t read the file!',304);//出错处理  
  134.                     exit;  
  135.               }  
  136.               return $string_str;  
  137.           }  
  138.    }  
  139.      
  140.    public function read($fp_res,$length_int)//文件资源标识,长度控制  
  141.    {  
  142.        //读入文件fp_res,最长为length_int  
  143.           if(!$string_str=fread($fp_res,$length_int))  
  144.           {  
  145.                  $this->on_error('Can\'t read the file!',305);//出错处理  
  146.                  exit;  
  147.           }  
  148.           return $string_str;  
  149.    }  
  150.      
  151.    public function is_exists($fileName_str)//文件名  
  152.    {  
  153.        //检查文件$fileName_str是否存在,存在则返回true,不存在返回false  
  154.           return file_exists($fileName_str);  
  155.    }  
  156.   
  157. /******************取得文件大小*********************/  
  158. /* 
  159. 取得文件fileName_str的大小 
  160. $fileName_str 是文件的路径和名称 
  161. 返回文件大小的值 
  162. */  
  163.    public function get_file_size($fileName_str)//文件名  
  164.    {  
  165.        return filesize($fileName_str);  
  166.    }  
  167.   
  168. /******************转换文件大小的表示方法*********************/  
  169. /* 
  170. $fileSize_int文件的大小,单位是字节 
  171. 返回转换后带计量单位的文件大小 
  172. */  
  173.    public function change_size_express($fileSize_int)//文件名  
  174.    {  
  175.        if($fileSize_int>1024)  
  176.        {  
  177.           $fileSizeNew_int=$fileSize_int/1024;//转换为K  
  178.           $unit_str='KB';  
  179.             if($fileSizeNew_int>1024)  
  180.              {  
  181.               $fileSizeNew_int=$fileSizeNew_int/1024;//转换为M  
  182.               $unit_str='MB';  
  183.              }  
  184.           $fileSizeNew_arr=explode('.',$fileSizeNew_int);  
  185.           $fileSizeNew_str=$fileSizeNew_arr[0].'.'.substr($fileSizeNew_arr[1],0,2).$unit_str;  
  186.        }  
  187.        return $fileSizeNew_str;  
  188.    }  
  189. /******************重命名文件*********************/  
  190. /* 
  191. 将oldname_str指定的文件重命名为newname_str 
  192. $oldName_str是文件的原名称 
  193. $newName_str是文件的新名称 
  194. 返回错误信息 
  195. */   
  196.    public function rename_file($oldName_str,$newName_str)  
  197.    {  
  198.           if(!rename($oldName_str,$newName_str))  
  199.           {  
  200.                  $this->on_error('Can\'t rename file!',308);  
  201.                  exit;  
  202.           }  
  203.    }  
  204.   
  205. /******************删除文件*********************/  
  206. /* 
  207. 将filename_str指定的文件删除 
  208. $fileName_str要删除文件的路径和名称 
  209. 返回错误信息 
  210. */  
  211.    public function delete_file($fileName_str)//  
  212.    {  
  213.           if(!unlink($fileName_str))  
  214.           {  
  215.                  $this->on_error('Can\'t delete file!',309);//出错处理  
  216.                  exit;  
  217.           }  
  218.    }  
  219.   
  220. /******************取文件的扩展名*********************/  
  221. /* 
  222. 取filename_str指定的文件的扩展名 
  223. $fileName_str要取类型的文件路径和名称 
  224. 返回文件的扩展名 
  225. */  
  226.    public function get_file_type($fileName_str)  
  227.    {  
  228.           $fileNamePart_arr=explode('.',$fileName_str);  
  229.           while(list(,$fileType_str)=each($fileNamePart_arr))  
  230.           {  
  231.            $type_str=$fileType_str;  
  232.           }  
  233.            return $type_str;  
  234.    }  
  235.   
  236. /******************判断文件是否是规定的文件类型*********************/  
  237. /* 
  238. $fileType_str规定的文件类型 
  239. $fileName_str要取类型的文件路径和名称 
  240. 返回false或true 
  241. */  
  242.    public function is_the_type($fileName_str,$fileType_arr)  
  243.    {  
  244.        $cheakFileType_str=$this->get_file_type($fileName_str);  
  245.        if(!in_array($cheakFileType_str,$fileType_arr))  
  246.        {  
  247.         return false;  
  248.           }  
  249.        else  
  250.        {  
  251.           return true;  
  252.        }  
  253.    }  
  254.   
  255. /******************上传文件,并返回上传后的文件信息*********************/  
  256. /* 
  257. $fileName_str本地文件名 
  258. $filePath上传文件的路径,如果$filePath是str则上传到同一目录用一个文件命名,新文件名在其加-1,2,3..,如果是arr则顺序命名 
  259. $allowType_arr允许上传的文件类型,留空不限制 
  260. $maxSize_int允许文件的最大值,留空不限制 
  261. 返回的是新文件信息的二维数组:$reFileInfo_arr 
  262. */  
  263.    public function upload_file($fileName_str,$filePath,$allowType_arr='',$maxSize_int='')  
  264. {        
  265.        $fileName_arr=$_FILES[$fileName_str]['name'];  //文件的名称  
  266.        $fileTempName_arr=$_FILES[$fileName_str]['tmp_name'];  //文件的缓存文件  
  267.        $fileSize_arr=$_FILES[$fileName_str]['size'];//取得文件大小  
  268.        $reFileInfo_arr=array();  
  269.        $num=count($fileName_arr)-1;  
  270.        for($i=0;$i<=$num;$i++)  
  271.       {  
  272.            if($fileName_arr[$i]!='')   
  273.         {  
  274.           if($allowType_arr!='' and !$this->is_the_type($fileName_arr[$i],$allowType_arr))//判断是否是允许的文件类型  
  275.           {  
  276.            $this->on_error('The file is not allowed type!',310);//出错处理  
  277.            break;  
  278.           }  
  279.   
  280.           if($maxSize_int!='' and $fileSize_arr[$i]>$maxSize_int)  
  281.           {  
  282.            $this->on_error('The file is too big!',311);//出错处理  
  283.            break;  
  284.           }  
  285.     
  286.           $j=$i+1;  
  287.           $fileType_str=$this->get_file_type($fileName_arr[$i]);//取得文件类型  
  288.           if(!is_array($filePath))  
  289.           {  
  290.           $fileNewName_str=$filePath.'-'.($j).'.'.$fileType_str;  
  291.           }  
  292.           else  
  293.           {  
  294.           $fileNewName_str=$filePath_arr[$i].'.'.$fileType_str;  
  295.           }  
  296.           copy($fileTempName_arr[$i],$fileNewName_str);//上传文件  
  297.           unlink($fileTempName_arr[$i]);//删除缓存文件  
  298.   
  299.           //---------------存储文件信息--------------------//  
  300.           $doFile_arr=explode('/',$fileNewName_str);  
  301.           $doFile_num_int=count($doFile_arr)-1;  
  302.           $reFileInfo_arr[$j]['name']=$doFile_arr[$doFile_num_int];  
  303.           $reFileInfo_arr[$j]['type']=$fileType_str;  
  304.           $reFileInfo_arr[$j]['size']=$this->change_size_express($fileSize_arr[$i]);  
  305.       }  
  306.    }  
  307.    return $reFileInfo_arr;  
  308. }  
  309.   
  310. /******************备份文件夹*********************/  
  311. }  
  312.   
  313. ?>  
复制代码
原创粉丝点击