zoukankan      html  css  js  c++  java
  • PHP文件系统操作常用函数

    虽然PHP提供很多内置的文件处理函数,但是分得特别细,有一些操作需要多个函数一起使用才能达到目标,比如删除非空文件夹的所有内容,遍历文件夹等功能,下面各个函数是学习的时候整理的,有的是教程里的,有的是自己想的,可以直接调用,免得自己再次写一遍,,本人是PHP菜鸟,一定存在很多bug,如果你愿意,非常欢迎留言,我收到留言后再修改。

     1     /**
     2      * [以文字形式返回文件的类型]
     3      * @param  [type] $filename [description]
     4      * @return [type]           [description]
     5      */
     6     function getfiletype($filename){
     7         $type=filetype($filename);
     8         switch($type){
     9             case "dir":
    10                 echo "this is a dir<br>";
    11                 break;
    12             case "file":
    13                 echo "this is a file<br>";
    14                 break;
    15             case "unknown":
    16                 echo "can't know the file<br>";
    17                 break;
    18         }
    19     }
     1     /**
     2      * [返回格式化后的文件大小]
     3      * @param  [type] $size [description]
     4      * @return [type]       [description]
     5      */
     6     function transfer_filesize($size){
     7         if($size>=pow(2,40)){
     8             return ceil($size/pow(2,40))."TB";
     9         } else if($size>=pow(2,30)){
    10             return ceil($size/pow(2,30))."GB";
    11         } else if($size>=pow(2,20)){
    12             return ceil($size/pow(2,20))."MB";
    13         } else if($size>=pow(2,10)){
    14             return ceil($size/pow(2,10))."KB";
    15         } else {
    16             return $size."B";
    17         }
    18     }
     1     /**
     2      * [获取文件夹或者文件的属性,包含文件类型及大小]
     3      * @param  [type] $filename [description]
     4      * @return [type]           [description]
     5      */
     6     function getfilepro($filename){
     7         if(file_exists($filename)){
     8             echo "the file is exists<br>";
     9             getfiletype($filename);
    10             echo "the file size is ".transfer_filesize(filesize($filename))."<br>";
    11         } else {
    12             echo "the file is not exists<br>";
    13         }
    14     }
     1     /**
     2      * [遍历整个文件夹]
     3      * @param  [string] $file [需要遍历的文件夹名或文件名]
     4      * @return [type]       [description]
     5      */
     6     function show_all_files($file){
     7         $dir=opendir($file);
     8         while($filename=readdir($dir)){
     9             if($filename!='.' && $filename!='..'){
    10                 $filename=$file."/".$filename;
    11                 if(is_dir($filename)){
    12                     echo "目录".$filename."<br>";
    13                     show_all_files($filename);
    14                 } else{
    15                     echo "文件".$filename."<br>";
    16                 }
    17             }
    18         }
    19         closedir($dir);
    20     }
    21     show_all_files("./class");
    
     1     /**
     2      * [获取一个磁盘或者分区的总容量和剩余容量]
     3      * @param  [string] $disk [description]
     4      * @return [array]       [可取消注释,返回一个包含两个值的数组]
     5      */
     6     function get_disk_space($disk){
     7         $total_space=transfer_filesize(disk_total_space($disk));
     8         $free_space=transfer_filesize(disk_free_space($disk));
     9         echo $disk."盘总容量为:".$total_space."<br>";
    10         echo $disk."盘可用容量为:".$free_space."<br>";
    11         return array($total_space,$free_space);
    12     }
    13     get_disk_space("C:");
     1     /**
     2      * [目录总数,文件总数,及目录总大小]
     3      * @param  [string] $filename [目录名]
     4      * @return [array]           [返回该目录的目录总数,文件总数,及目录总大小]
     5      */
     6     function get_all_nums($filename){
     7         $total_size=0;
     8         $total_dir_nums=0;
     9         $total_file_nums=0;
    10         function get_nums($filename){
    11             global $total_dir_nums,$total_file_nums,$total_size;
    12             $dir=opendir($filename);
    13             while($file=readdir($dir)){
    14                 if($file!="." && $file!=".."){
    15                     $file=$filename."/".$file;
    16                     if(is_dir($file)){
    17                         get_nums($file);
    18                         $total_dir_nums++;
    19                     } else {
    20                         $total_file_nums++;
    21                         $total_size+=filesize($file);
    22                     }
    23                 }
    24             }
    25             closedir($dir);
    26             return array($total_dir_nums,$total_file_nums,$total_size);
    27         }
    28         list($dir,$file,$size)=get_nums($filename);
    29         echo "文件数为:".$file."<br>";
    30         echo "目录数为:".$dir."<br>";
    31         echo "总大小为:".transfer_filesize($size)."<br>";
    32     }
    33     get_all_nums("./class");
     1     /**
     2      * [删除一个非空的目录或文件]
     3      * @param  [string] $filename [要删除的目录或文件名]
     4      * @return [null]           [null]
     5      */
     6     function delete_dir($filename){
     7         if(!file_exists($filename)){
     8             die("不存在该目录");
     9         }
    10         if(is_file($filename)){
    11             unlink($filename);
    12             echo "成功删除文件".$file."<br>";
    13         }
    14         $dir=opendir($filename);
    15         while($file=readdir($dir)){
    16             if($file!="." && $file!=".."){
    17                 $file=$filename."/".$file;
    18                 if(is_dir($file)){
    19                     delete_dir($file);
    20                 } else {
    21                     unlink($file);
    22                     echo "成功删除文件".$file."<br>";
    23                 }
    24             }
    25         }
    26         closedir($dir);
    27         rmdir($filename);
    28         echo "成功删除目录".$filename."<br>";
    29     }
    30     delete_dir("./aaaa");
     1 /**
     2        * [用来拷贝一个目录] 
     3        * @param  [string]   [$src_dir源目录]
     4        * @param  [string]  [$dest_dir目标目录]
     5        * @return [null]           [description]
     6        */
     7 
     8     function copyDir($src_dir,$dest_dir){
     9         //源目录是否存在不存在
    10         if(!file_exists($src_dir)){
    11             echo "the src dir is not exists
    ";
    12             return ;
    13         }
    14         //判断源目录是不是一个文件,若是文件,则直接复制,然后函数结束
    15         if(is_file($src_dir)){
    16             copy($src_dir,$dest_dir);
    17             return;
    18         }
    19         if(!file_exists($dest_dir)){
    20             mkdir($dest_dir);
    21         }
    22 
    23         if($dir_handle=opendir($src_dir)){
    24             while($filename=readdir($dir_handle)){
    25                 if($filename!="." && $filename!=".."){
    26                     $sub_src_file=$src_dir."/".$filename;
    27                     $sub_dest_file=$dest_dir."/".$filename;
    28                     if(is_dir($sub_src_file))
    29                         copyDir($sub_src_file,$sub_dest_file);
    30                     if(is_file($sub_src_file))
    31                         copy($sub_src_file,$sub_dest_file);
    32                 }
    33             }
    34             closedir($dir_handle);
    35         }
    36     }
    37      copyDir("aaa","bbb");
     1     /**
     2      * [逐个字符读出文件所有内容]
     3      * @param  [string] $filename [要读的文件名]
     4      * @return [null]           [description]
     5      */
     6     function get_contents_one($filename){
     7         $fp=fopen($filename,"r");
     8         while(!feof($fp)){
     9             echo fgetc($fp);
    10         }
    11         echo "<br>";
    12         fclose($fp);
    13     }
    14     get_contents_one("bbb.txt");
     1     /**
     2      * [逐行读出文件所有内容]
     3      * @param  [string] $filename [要读的文件名]
     4      * @return [null]           [description]
     5      */
     6     function get_contents_two($filename){
     7         $fp=fopen($filename,"r");
     8         while(!feof($fp)){
     9             echo fgets($fp);
    10         }
    11         echo "<br>";
    12         fclose($fp);
    13     }
    14     get_contents_two("bbb.txt");
  • 相关阅读:
    C# 文件类的操作---删除
    C#实现Zip压缩解压实例
    UVALIVE 2431 Binary Stirling Numbers
    UVA 10570 meeting with aliens
    UVA 306 Cipher
    UVA 10994 Simple Addition
    UVA 696 How Many Knights
    UVA 10205 Stack 'em Up
    UVA 11125 Arrange Some Marbles
    UVA 10912 Simple Minded Hashing
  • 原文地址:https://www.cnblogs.com/-beyond/p/7204543.html
Copyright © 2011-2022 走看看