首页 > 代码库 > PHP文件操作系统----主要的文件操作函数

PHP文件操作系统----主要的文件操作函数

一、文件操作系统概述

  1.概述:

    php中的文件操作系统主要是对文件和目录的操作。文件在windows系统下分为3种不同:文件、目录、未知,在linux/unix系统下分为7种不同:block、char、dir、fifo、file、link和unknown七种类型。目录在windows系统:D:/a/b/c.php 或 D:\a\b\c.php,在linux/unix系统: /a/b/c.php,为统一建议使用"/"作为目录之间的分割符。

  2.目录或文件属性获取函数

  *(1)取得文件类型string filetype ( string $filename );可能的值有 fifo,char,dir,block,link,file 和 unknown

<?php      echo filetype(‘test.php‘);//返回file    echo filetype(‘D:/wamp/www/face‘);//返回dir    echo filetype(‘face1‘);//不存的文件或目录抛出警告    echo filetype(‘face‘);//返回dir?>

  *(2)判断给定文件名是否是一个目录:bool is_dir(string $filename); 如果文件名存在,并且是个目录,返回 TRUE ,否则返回 FALSE

<?php    var_dump(is_dir(‘test.php‘));//boolean false    var_dump(is_dir(‘face‘));//boolean true    var_dump(is_dir(‘c:‘));//boolean true?>    

   (3)判断给定文件名是否可执行: bool is_executable(string $filename);如果文件存在且可执行则返回 TRUE ,错误时返回 FALSE 。这个函数在window下没有测试出来,老是false

   *(4)判断给定文件名是否为一个正常的文件: bool is_file(string $filename);如果文件存在且为正常的文件则返回 TRUE ,否则返回 FALSE

<?php     var_dump(is_file(‘test.php‘));//boolean true?>

 

  (5)判断给定文件名是否为一个符号连接: bool is_link(string $filename);如果文件存在并且是一个符号连接则返回 TRUE ,否则返回 FALSE-----未测试

  (6)判断给定文件名是否可读: bool is_readable(string $filename);如果由 filename 指定的文件或目录存在并且可读则返回 TRUE ,否则返回 FALSE

<?php     var_dump(is_readable(‘D:/wamp/www/test.php‘));//boolean true?>

 

  *(7)判断给定文件名是否可读: bool is_writable(string $filename);如果由 filename 指定的文件或目录存在并且可读则返回 TRUE ,否则返回 FALSE

<?php     var_dump(is_writable(‘D:/wamp/www/test.php‘));//boolean true?>

 

  *(8)检查文件或目录是否存在: bool file_exists(string $filename);如果由 filename 指定的文件或目录存在则返回 TRUE ,否则返回 FALSE   

<?php     var_dump(file_exists(‘test.php‘));//boolean true    var_dump(file_exists(‘face‘));//boolean true?>

 

  *(9)取得文件大小: int filesize(string $filename);返回文件大小的字节数,如果出错返回 FALSE 并生成一条 E_WARNING 级的错误。

<?php     var_dump(filesize(‘test.php‘));//int 44---字节数    var_dump(filesize(‘face‘));//int 12288    //可以是文件或文件夹?>

 

  *(10)取得文件的 inode 修改时间:int filectime(string $filename);返回文件上次 inode 被修改的时间, 或者在失败时返回 FALSE 。 时间以 Unix 时间戳的方式返回

<?php    //获取的是文件的创建时间,文件和文件夹都行    date_default_timezone_set(‘Asia/Shanghai‘);     $time=filectime(‘face‘);    echo date(‘Y-m-d H:i:s‘,$time);?>

   *(11)取得文件修改时间:int filemtime(string $filename);返回文件上次被修改的时间, 或者在失败时返回 FALSE 。时间以 Unix 时间戳的方式返回

   *(12)取得文件访问时间:int fileatime(string $filename);返回文件上次被访问的时间, 或者在失败时返回 FALSE 。时间以 Unix 时间戳的方式返回

<?php    header(‘content-type:text/html;charset=utf-8;‘);    //获取的是文件的创建 修改时间,文件和文件夹都行    date_default_timezone_set(‘Asia/Shanghai‘);     $time1=filectime(‘face‘);    $time2=filemtime(‘face‘);    $time3=fileatime(‘face‘);    echo ‘文件创建的时间:‘,date(‘Y-m-d H:i:s‘,$time1);    echo "<br/>";    echo ‘文件最后修改的时间:‘,date(‘Y-m-d H:i:s‘,$time2);    echo "<br/>";    echo ‘文件最后访问的时间:‘,date(‘Y-m-d H:i:s‘,$time2);    //这个时候文件夹的访问和修改时间一样    echo ‘<br/>‘,‘-----------------------------------------------------------------‘,‘<br/>‘;    $time1=filectime(‘a.txt‘);    $time2=filemtime(‘a.txt‘);    $time3=fileatime(‘a.txt‘);    echo ‘文件创建的时间:‘,date(‘Y-m-d H:i:s‘,$time1);    echo "<br/>";    echo ‘文件最后修改的时间:‘,date(‘Y-m-d H:i:s‘,$time2);    echo "<br/>";    echo ‘文件最后访问的时间:‘,date(‘Y-m-d H:i:s‘,$time2);?>

 

结果:

技术分享

注意:

  关于访问 修改 新建这个三个时间的解释

  一般来说,比较有参考价值的是修改时间,表示文件内容被修改过,会记录一个时间。通过复制/粘贴、重命名操作都不会更改修改时间。复制/粘贴操作只修改创建时间/访问时间重命名不会改变任何时间。(xp系统下测试结果)

     按照道理讲,访问时间应该是最后一次读文件的时间。但对一个文件只是读操作的话,为了更新这个时间,就必须做一个写操作,把该信息写入到文件属性和目录索引中去,这样一个读操作就变成了一个读+写操作。由于现在的硬盘都非常大,几十万个文件都是常事,如果读操作再加一个写操作,那么这个确实影响性能。(详细可以参考Fsutil: behavior 中对于DisableLastAccess的描述,地址附后)。

     从 XP2003 SP1起,为了性能,默认把LastAccessTime(最后访问时间)给禁用了。只有在修改文件时,才会更新访问时间。

  下图是各个文件系统对时间的支持情况。

  技术分享

  (13)给出文件的信息: array stat(string $filename);获取由 filename 指定的文件的统计信息。如果 filename 是符号连接,则统计信息是关于被连接文件本身的,而不是符号连接。lstat() 和 stat() 相同,只除了它会返回符号连接的状态。

  技术分享

 记忆:参数都是string $filename;不是文件就是目录;

   目录文件和存在,可读可写可执行,创建修改和访问,文件大小和类型!

重点区分:

    PHP的 file_exists = is_dir + is_file
    它既可以判断文件是否存在,又可以判断目录是否存在。但这样一个全面的函数执行效率非常低,就像asp中request不指定是form,还是get,cookies,所以结论是:


    ?如果要判断目录是否存在,请用独立函数 is_dir(directory)
    ?如果要判断文件是否存在,请用独立函数 is_file(filepath)
    is_file 只判断文件是否存在;
    file_exists 判断文件是否存在或者是目录是否存在;
    is_dir 判断目录是否存在;


    查看手册,虽然这两个函数的结果都会被缓存,但是is_file却快了N倍。
    还有一个值得注意的:
    文件存在的情况下,is_file比file_exists要快N倍;
    文件不存在的情况下,is_file比file_exists要慢;
    结论是,file_exits函数并不会因为该文件是否真的存在而影响速度,但是is_file影响就大了

二、目录的相关操作

  1.路径相关函数

    *(1)返回路径中的文件名部分: string basename("url地址"[,去除部分])

<?php    $path="D:wamp/www/test.php";    echo basename($path);//test.php    echo "<br/>";    echo basename($path,‘php‘);//test.?> 

    *(2)返回路径中的目录部分:string dirname(string $path);

<?php    $path="D:wamp/www/test.php";    echo dirname($path);//D:wamp/www?>

     *(3)返回文件路径的信息: array pathinfo(string $path);

<?php    $path="D:wamp/www/test.php";    var_dump(pathinfo($path));?>

 

    结果

      技术分享

     (4)获取指定文件的绝对路径: string realpath(string $path);

<?php    $path="test.php";    echo realpath($path);//D:wamp/www/test.php?>

 

  2.目录的遍历函数

    *(1)打开一个目录,返回此目录的资源句柄: resource opendir(string $path);

<?php    $path="D:/wamp/www/";//必须是一个合法且存在的目录    echo opendir($path);//Resource id #3?>

 

    *(2)从目中读取一个目录或文件,并指针向下移动一位: string readdir(resource $dir_handle);

<?php    header(‘content-type:text/html;charset=utf-8;‘);    $path="D:/wamp/www";    if(!is_dir($path)){        echo "不是一个合法的文件路径!";        exit;    }    if($dir_handle=opendir($path)){        echo "打开资源成功!"."<br/>";        while(false!==($file=readdir($dir_handle))){//必须严格按照这种方式来遍历            echo $file,‘<br/>‘;        }    }else{        echo "打开资源失败!";    }
  closedir($dir_path);//关闭资源
?>

   结果:

    技术分享

 

    *(3)关闭目录句柄:void closedir(resource $dir_handle);

    (4) 倒回目录指针(移至首位)----rewinddir(资源句柄);

  3.统计空间的大小

    (1)返回目录中的可用空间:float disk_free_space ( string $directory ),以浮点返回可用的字节数, 或者在失败时返回 FALSE

    (2)返回一个目录的磁盘总大小:float disk_total_space ( string $directory ),以浮点返回一个目录的磁盘总大小字节数, 或者在失败时返回 FALSE

<?php    echo disk_free_space(‘D:‘);//122210635776    echo "<br/>";    echo disk_free_space(‘./‘);//122210635776    echo "<br/>";    echo disk_total_space(‘./‘);//141394038784?>

 

  4.目录的操作

    *(1)尝试新建一个由 pathname 指定的目录:bool mkdir(string $path);成功时返回 TRUE , 或者在失败时返回 FALSE

    *(2)尝试删除 dirname 所指定的目录(该目录必须是空的,而且要有相应的权限): bool rmdir(string $path);成功时返回 TRUE , 或者在失败时返回 FALSE

<?php      header(‘content-type:text/html;charset=utf-8;‘);    if(mkdir(‘D:/wamp/www/www1/www1‘)){        echo "创建成功!";    }else{        echo "创建失败!";    };    if(rmdir(‘D:/wamp/www/www1/www1‘)){//该文件必须为空        echo "删除成功!";    }else{        echo "删除失败!";    }?>

 

     *(3)删除文件bool unlink ( string $filename);成功时返回 TRUE , 或者在失败时返回 FALSE  

<?php      header(‘content-type:text/html;charset=utf-8;‘);    if(unlink(‘D:/wamp/www/a.txt‘)){        echo "删除成功!";    }else{        echo "删除失败!";    }?>

 

    (4)拷贝文件: bool copy ( string $source , string $dest );

<?php      header(‘content-type:text/html;charset=utf-8;‘);    // 第一个参数是复制的源,第二个参数是复制到的路径和名称,不能单纯的是个路径    if(copy(‘D:/wamp/www/aa.jpg‘, ‘D:/wamp/www/w/aa1.jpg‘)){        echo "复制成功!";    }else{        echo "复制失败!";    }?>

 

 三、文件基本操作

   1.打开文件:resource fopen ( string $filename , string $mode )

 技术分享

  2.读取文件内容:string fread ( resource $handle , int $length )返回所读取的字符串, 或者在失败时返回 FALSE

<?php      $path=‘D:/wamp/www/a.txt‘;    if($handle=fopen($path,‘rb‘)){        $content=fread($handle, filesize($path));        echo $content;    }else{        echo "打开资源失败!";    }?>

 

  3.写入文件:int fwrite ( resource $handle , string $string [, int $length ] );fwrite() 返回写入的字符数,出现错误时则返回 FALSE

   这个地方可以验证不同的打开模式

  4.关闭一个已打开的文件资源:bool fclose ( resource $handle )

  5.从文件指针中读取一行:string fgets ( resource $handle [, int $length ] )

  6.从文件指针中读取字符string fgetc ( resource $handle )

  读取文件注意事项:读取之后“光标”就会停留,注意理解这一点。最后两种结果的不同

示例:读取文件内容

技术分享

<?php      $path=‘D:/wamp/www/a.txt‘;    $handle=fopen($path,‘r‘);    echo fgets($handle);?>

 

结果1:

技术分享

<?php      $path=‘D:/wamp/www/a.txt‘;    $handle=fopen($path,‘r‘);    echo fread($handle, filesize($path));?>

 

结果2:

技术分享

<?php      $path=‘D:/wamp/www/a.txt‘;    $handle=fopen($path,‘r‘);    echo fgets($handle);    echo "<br/>","--------","<br/>";    echo fread($handle, filesize($path));?>

 

结果3:

技术分享

<?php      $path=‘D:/wamp/www/a.txt‘;    $handle=fopen($path,‘r‘);    echo fread($handle, filesize($path));    echo "<br/>","--------","<br/>";    echo fgets($handle);?>

 

结果4:

技术分享

  不用打开文件直接操作文件的函数

  7.将文件读出(内容是数组格式),并返回.把整个文件读入一个数组中:array file ( string $filename)

<?php      $path=‘D:/wamp/www/a.txt‘;    $arr=file($path);    print_r($arr);?>

 

 技术分享

  8.将文件内容读出,并输出,读入一个文件并写入到输出缓冲:int readfile ( string $filename );返回字节数

<?php      $path=‘D:/wamp/www/a.txt‘;    readfile($path);?>

 

  技术分享

  9.10

    string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )The function returns the read data 或者在失败时返回 FALSE
    int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )该函数将返回写入到文件内数据的字节数,失败时返回 FALSE
<?php      $contents=file_get_contents(‘http://www.baidu.com‘);    $path="D:/wamp/www/a.txt";    file_put_contents($path, $contents);?>

 

结果:

技术分享

  修改文件名:bool rename ( string $oldname , string $newname [, resource $context ] );

其他

技术分享

PHP文件操作系统----主要的文件操作函数