PHP经常使用函数之文件系统处理

检测

  • 检查文件或目录是否存在windows

    file_exists ()
  • 检查给定文件名是否为一个存在的文件(存在、文件)函数

    is_file ( string $filename )
  • 检查给定目录名是否为一个存在的目录(存在、目录)编码

    is_dir ( string $filename )
  • 判断给定的文件名或目录名是否存在且可读(存在、文件或目录、可读)code

    is_readable ( string $filename )
  • 判断给定的文件名或目录名是否存在且可写(存在、文件或目录、可写)递归

    is_writable ( string $filename )

路径解析

  • 解析文件名内存

    basename ( string $path [, string $suffix ] ) //包含有指向一个文件的全路径的字符串utf-8

  • 解析目录名rem

    dirname ( string $path ) //包含有指向一个文件的全路径的字符串字符串

  • 解析全路径get

    pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] )

目录操做

  • 新建目录

    mkdir (); //建立目录,第三个参数表示是否递归建立
  • 删除目录

    rmdir (); //只能删除空目录,非空目录必须使用递归删除
    
    function removeDirOrFile($path){
        if(is_file($path)){
            return unlink($path);
        }
        
        if(is_dir($path)){
            $dir_handle = opendir($path);
            while(false !== ($file = readdir($dir_handle))) {
                if($file === '.' || $file === '..') continue;
                $subPath = $path.DIRECTORY_SEPARATOR.$file;
                $fnname = __FUNCTION__;
                $fnname($subPath);
            }
            closedir($dir_handle);
            return rmdir($path);
        }
        
        return FALSE;
    }
  • 移动/重命名目录

    rename ( string $oldname , string $newname [, resource $context ] );

  • 获取目录内容

    opendir();
    readdir();
    closedir();
    rewind();
    
    function readDirsTree($path,$deep=0){
        
        if(is_file($path)){
            exit(basename($path));
        }
        
        if(is_dir($path)){
            $dir_handle = opendir($path);
            while(false !== ($file = readdir($dir_handle))) {
                if($file === '.' || $file === '..') continue;
                
                echo str_repeat('&nbsp;',$deep*2).iconv('GB2312','UTF-8',$file).'<br/>';
                
                if(is_dir($path.DIRECTORY_SEPARATOR.$file)){
                    $fnname = __FUNCTION__;
                    $fnname($path.DIRECTORY_SEPARATOR.$file, $deep+1);
                }
            }
            closedir($dir_handle);
        }
     }
  • 复制目录

    function copyDir($dirFrom, $dirTo){
        if(is_dir($dirFrom)){
            if(!file_exists($dirTo)){
                mkdir($dirTo,0777,TRUE);
            }
            
            $dir_handle = opendir($dirFrom);
            while(false !== ($file = readdir($dir_handle))) {
                if($file === '.' || $file === '..') continue;
                $fromPath = $dirFrom.DIRECTORY_SEPARATOR.$file;
                $toPath = $dirTo.DIRECTORY_SEPARATOR.$file;
                
                if(is_file($fromPath)){
                    copy($fromPath, $toPath);
                }
                
                if(is_dir($fromPath)){
                    $fnname = __FUNCTION__;
                    $fnname($fromPath, $toPath);
                }
                
            }
            closedir($dir_handle);
            return TRUE;
        }else{
            return FALSE;
        }
    }

文件操做

  • 获取文件大小

    filesize ( string $filename );

  • 删除文件

    unlink ( string $filename);

  • 剪切/重命名文件

    rename ( string $oldname , string $newname );

  • 拷贝文件

    copy ( string $source , string $dest );

  • 写文件

    file_put_contents ( string $filename , mixed $data [, int $flags = 0 ] );
    通常写文件就直接使用这个函数,里面其实也是依次调用fopen(),fwrite()以及 fclose() 功能。

  • 读文件
    file_get_contents ( string $filename );
    此函数只适合读一些小文件(文件大小很小的),若是读大文件,必须使用下面方法,不然内存很容易溢出

    fopen ( string $filename , string $mode );
    fread ( resource $handle , int $length ); //按字节数读取
    fgets ( resource $handle [, int $length ] ); //默认长度为1kb,按行读取
    fgetc ( resource $handle ); //按1个字节1个字节读取
    fclose ( resource $handle );

  • 获取文件修改时间
    filemtime ( string $filename ); //返回时间戳

编码问题

  • 在windows下,获取含有中文的目录名或文件名时,因为中文是GBK编码,而项目是utf-8编码,因此必须转码iconv('GBK','utf-8',$filename);

  • 当输入的路径含有中文,因为项目是utf-8,而系统文件名或目录名都是GBK编码,因此必须转为iconv('utf-8','GBK',$path);

相关文章
相关标签/搜索