php操作目录文件

php提供对服务器中的目录进行操作的函数,操作包括创建、读取、复制、删除。

php函数对于目录的解析支持绝对路径和相对路径。这写函数有basename()、dirname()、pathinfo()、opendir()、readdir()、closedir()、rewinddir()等。具体见下面代码:

<?php
    /**
     * @author youthflies
     * 文件目录操作
     */
    header("Content-Type:text/html;charset=utf-8");
         
    $filePath = "/home/youthflies/downloads/jdk-6u37-linux-i586.bin";
    if(file_exists($filePath))
        echo "绝对路径正确!" ."<br />";
         
    if(file_exists("../../../../downloads/jdk-6u37-linux-i586.bin"))
        echo "相对路径正确!" . "<br />";
             
    //basename()函数返回路径中的文件名,第二个参数为可选,如果手动输入了拓展名,该函数就返回文件名(不带拓展名)    
    echo basename($filePath) . "<br />";
    echo basename($filePath, ".bin") . "<br />";
         
    //dirname()与basename()相反,它返回给定文件的路径部分
    echo dirname($filePath) . "<br />";
         
    //pathinfo()函数返回一个关联数组,包括路径名、文件名、基本名和拓展名,键名分别是dirname、basename、filename和extension
    print_r(pathinfo($filePath));
    echo "<br />";
         
    /**
     * 如果要遍历某个路径,可以用opendir()、readdir()、closedir()和rewinddir()函数
     * opendir()函数,接受一个目录作为参数,返回一个目录句柄,如果失败则返回false
     * readdir()函数,接受opendir()函数返回的句柄作为参数,函数返回该目录句柄的一个文件,且指针向后移动,通常通过循环来遍历目录的全部内容
     * closedir()接受opendir()函数返回的句柄作为参数,来关闭打开的目录
     * rewinddir()接受opendir()函数返回的句柄作为参数,将目录指针重置到开始处,即目录的开头
     */
    $dirName = "/home/youthflies/downloads";
    $dirHandle = opendir($dirName);
    echo '<table border="0" align="center" width="600" cellspacing="0" cellpadding="0">';
    echo '<tr align ="left" bgcolor="blue">';
    echo '<th>文件名</th><th>文件大小</th><th>文件类型</th><th>修改时间</th></tr>';
    $num = 0;
    while($fileName = readdir($dirHandle))
    {
        $file = $dirName . '/' . $fileName;
        $bgcolor = $num++%2==0 ? "blue" : "";
        echo '<tr bgcolor=' . $bgcolor . '>';
        echo '<td>' . $file . '</td>';
        echo '<td>' . filetype($file) . '</td>';
        echo '<td>' . filesize($file) . '</td>';
        echo '<td>' . date("Y-m-d h:i:s", filemtime($file)) . '</td>';
        echo '</tr><tr>';
    }
    echo '</table>';
    closedir($dirHandle);
         
    /**
     * 下面是个计算目录大小的例子(使用的递归)
     */
    function dirSize($dirName)
    {
        $dir_Size = 0;
        if(is_file($dirName))
            return filesize($dirName);
        $dirHandle = opendir($dirName);
        if($dirHandle == false)
            return;
        while($fileName = readdir($dirHandle))
        {
            if($fileName!='.' && $fileName!='..')
            {
                $file = $dirName . '/' . $fileName;
                if(is_file($file))
                    $dir_Size += filesize($file);
                elseif (is_dir($file))
                    $dir_Size += dirSize($file);
            }
        }
        closedir($dirHandle);
        return $dir_Size;
    }
    //调用dirSize()函数计算downloads目录的大小
    echo dirSize("/home/youthflies/downloads")/(1024*1024) . "MB" . "<br />";
         
    //disk_total_space()和disk_free_space()函数分别用来计算当前磁盘的总大小和剩余大小
    echo disk_total_space("/home")/(1024*1024) . "MB" . "<br />";
    echo disk_free_space("/home")/(1024*1024) . "MB" . "<br />";
         
    //mkdir()函数用于创建目录;rmdir()函数用于删除空的目录,如果要删除非空目录,就要先递归删除该目录的其他文件;删除文件使用unlink()函数
    $dirName2 = "/home/youthflies/downloads/mktem";
    if(file_exists($dirName2))
    {
        echo "目录已经存在" . "<br />";
        if(rmdir("/home/youthflies/downloads/mktem"))
            echo "删除/home/youthflies/downloads/mktem 成功" . "<br />";
    }
    else
    {
        if(mkdir($dirName2))
        {
            echo "创建 /home/youthflies/downloads/mktem 成功";
            //删除mktem目录下的tem.txt文件
            exec("touch tem.txt"); //最好不要直接调用操作系统的shell命令
            if(file_exists("mktem/tem.txt"))
                unlink("tem.txt", "mktem");
        }
    }
    //如果想要复制文件,可以使用copy()函数,该函数只能复制普通文件,如果想复制整个文件夹,要使用递归。
    copy("/home/youthflies/downloads/source.txt", "/home/youthflies/downloads/destination.txt");
?>

上面代码输入结果:

绝对路径正确!
相对路径正确!
jdk-6u37-linux-i586.bin
jdk-6u37-linux-i586
/home/youthflies/downloads
Array(    [dirname] => /home/youthflies/downloads    [basename] => jdk-6u37-linux-i586.bin    [extension] => bin    [filename] => jdk-6u37-linux-i586)

文件名文件大小文件类型修改时间/home/youthflies/downloads/百度Java Web安全编码规范(2012修改).pdffile4607442012-11-20 01:15:03
/home/youthflies/downloads/source.txtfile02012-11-28 02:21:54
/home/youthflies/downloads/mktemdir40962012-11-28 03:30:00
/home/youthflies/downloads/node-v0.8.14dir40962012-11-25 10:14:26
/home/youthflies/downloads/eclipsedir40962012-09-20 04:40:18
/home/youthflies/downloads/linkapache-tomcat-7.0.33.tar.gzlink76960042012-11-28 10:01:40
/home/youthflies/downloads/eclipse-jee-juno-SR1-linux-gtk.tarfile2672435202012-11-19 11:43:15
/home/youthflies/downloads/jdk-6u37-linux-i586.binfile717640732012-11-27 02:13:01
/home/youthflies/downloads/selenium-server.jarfile161750992012-11-22 06:53:56
/home/youthflies/downloads/.dir40962012-11-28 03:30:00
/home/youthflies/downloads/google-chrome-stable_current_i386.debfile343789882012-11-19 11:12:23
/home/youthflies/downloads/无标题文档~file02012-11-27 07:23:07
/home/youthflies/downloads/burpsuite_v1.4.01dir40962038-01-19 11:14:07
/home/youthflies/downloads/bnac-ubuntu-1.0.1.tar.gzfile77992012-11-19 08:05:28
/home/youthflies/downloads/apache-tomcat-7.0.33dir40962012-11-26 04:19:28
/home/youthflies/downloads/无标题文档file3192012-11-27 07:24:44
/home/youthflies/downloads/apache-tomcat-7.0.33.tar.gzfile76960042012-11-28 10:01:40
/home/youthflies/downloads/destination.txtfile02012-11-28 03:30:00
/home/youthflies/downloads/soapUI-x32-4.5.1.shfile1347171902012-11-26 02:37:44
/home/youthflies/downloads/..dir40962012-11-28 06:52:40
/home/youthflies/downloads/davmail_4.1.0-2042-1_all.debfile50549422012-11-23 01:55:49
/home/youthflies/downloads/ideaIU-11.1.4.tar.zipfile1939174812012-11-22 04:56:52
/home/youthflies/downloads/百度web安全测试_2012_inf开放日.pptxfile4593132012-11-28 12:07:33
/home/youthflies/downloads/xampp-linux-1.8.1.tar.gzfile847330452012-11-20 12:26:43
1150.3873596191MB
63830.78515625MB
41001.578125MB
目录已经存在
删除/home/youthflies/downloads/mktem 成功

你可能感兴趣的:(PHP,代码)