凯哥的文件管理系统

<?php
        $cur=empty($_GET["fname"]) ? dirname(str_replace("\\", "/",__FILE__)) : $_GET["fname"];
        $pre=dirname($cur);       
?>
<?php       
    switch($_GET['action']){
        case 0:
        case 5:
            yemian($cur);
            break;
        case 1:           
            echo "<center><h1>创建新的文件或目录</h1></center><hr/>";
            echo "<form method=\"post\" action=\"#\" enctype=\"multipart/form-data\"><input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"500000\" />"."上传文件:"."<input type='file' name='name' value='浏览' />";
            echo "<input type='submit' name='sub' value='上传'/><a href='system.php'>取消</a></form>";
            $fum=$_FILES['name']['error']; //获取错误信息
            $ftype=$_FILES['name']['type']; //获取上传文件的类型     
            $fhz=$_FILES['name']['name'];  //获取上传文件的后缀名
            $fsize=$_FILES['name']['size']; //获取文件的大小
            $ftmpname=$_FILES['name']['tmp_name']; //获取文件上传时的临时文件所在位置   
            if($_FILES['name']!=null){
                upload($fnum,$ftype,$fhz,$fsize,$ftmpname);  //当有上传文件的时候调用上传的函数
            }       
            break;
        case 2:
            echo "<center><h1>创建新的文件或目录</h1></center><hr/>";
            echo '<form method="post" action="#">'."目录名:"."<input type='text'name='dirname'/>";
            echo "<input type='submit' name='sub' value='创建'/><a href='system.php'>取消</a></form>";           
            if($_POST['dirname']!=""){
                mkdir($_POST['dirname']);
                echo "创建目录成功!";
            }elseif($_POST['dirname']==""){
                echo "请输入一个要创建的新目录名!";
            }
            break;
        case 3:
            echo "<center><h1>创建新的文件</h1><hr/>";
            echo '<form method="post" action="#">'."文件名:"."<input type='text'name='filename'  /><br/>";
            echo '<textarea name="content" rows="40" cols="160"></textarea><br/>';
            echo "<input type='submit' name='sub' value='保存文件'/><input type='reset' value='重置'/><a href='system.php'>取消</a></center></form>";
            $filename=$_POST['filename'];
            $content=$_POST['content'];
            if($filename!="" && $content!=""){
                $op=opendir($cur);
                while($file=readdir($op)){
                    if($filename!=$file){
                        file_put_contents($filename.".txt",$content);
                    }
                }
                closedir($op);
            }
            break;
        case 4:
            yemian($pre); //转到上级目录
            break;
        case 6:
            list($gendir)=explode(":",$cur); //回到文档根目录
            $gen=$gendir.":/";
            yemian($gen);
            break;
        case 7:
            //复制function copyDir($subdir,$newdir)
//            $str=ssdir($cur); //获取当前目前下的所有目录及文件的信息
//            $arrfile=$arrdir=array(); //定义两个空的数组
//            $arr=explode("##",$str);
//            $arrdir=explode("|",$arr[0]);
//            $arrfile=explode("|",$arr[1]);
            $fname=$_GET['fname'];
            $ftype=$_GET['ftype'];
            $fsize=$_GET['fsize'];
            $fmtime=$_GET['fmtime'];
            echo "<center><h1>文件或目录操作</h1></center><hr/>";
            echo '<form method="post" action="#">';   
            echo '<table width="300" >
            <tr>
                <td>文件名称:</td>
                <td >'.$fname.'</td>
            </tr>
            <tr>
                <td>文件类型:</td>
                <td>'.$ftype.'</td>
            </tr>
            <tr>
                <td>文件大小:</td>
                <td>'.$fsize.'</td>
            </tr>
            <tr>
                <td>文件修改时间:</td>
                <td>'.$fmtime.'</td>
            </tr>
            </table>';
            echo "将文件 ".$fname." 复制到:"."<input type='text'name='dirname'/>";
            echo "<input type='submit' name='sub' value='复制'/><a href='system.php'>取消</a></form>";
            $newdir=$_POST['dirname'];
            $subdir=$fname;
            if($newdir!=""){
                copyDir($subdir,$newdir);
                echo "复制成功!";
            }
            break;
        case 8:
             //重命名
            $fname=$_GET['fname'];
            $ftype=$_GET['ftype'];
            $fsize=$_GET['fsize'];
            $fmtime=$_GET['fmtime'];
            echo "<center><h1>文件或目录操作</h1></center><hr/>";
            echo '<form method="post" action="#">';   
            echo '<table width="300" >
            <tr>
                <td>文件名称:</td>
                <td >'.$fname.'</td>
            </tr>
            <tr>
                <td>文件类型:</td>
                <td>'.$ftype.'</td>
            </tr>
            <tr>
                <td>文件大小:</td>
                <td>'.$fsize.'</td>
            </tr>
            <tr>
                <td>文件修改时间:</td>
                <td>'.$fmtime.'</td>
            </tr>
            </table>';
            echo "将文件 ".$fname." 重命名为:"."<input type='text'name='rename'/>";
            echo "<input type='submit' name='sub' value='重命名'/><a href='system.php'>取消</a></form>";
            $newname=$_POST['rename'];
            $name=$fname;
            if($newname!=""){
                rname($name,$newname);
            }
            break;
        case 9:
             //删除
            $fname=$_GET['fname'];
            $ftype=$_GET['ftype'];
            $fsize=$_GET['fsize'];
            $fmtime=$_GET['fmtime'];
            echo "<center><h1>文件或目录操作</h1></center><hr/>";
            echo '<form method="post" action="#">';   
            echo '<table width="300" >
            <tr>
                <td>文件名称:</td>
                <td >'.$fname.'</td>
            </tr>
            <tr>
                <td>文件类型:</td>
                <td>'.$ftype.'</td>
            </tr>
            <tr>
                <td>文件大小:</td>
                <td>'.$fsize.'</td>
            </tr>
            <tr>
                <td>文件修改时间:</td>
                <td>'.$fmtime.'</td>
            </tr>
            </table>';
            echo "将文件 ".$fname."  ";
            echo "<input type='submit' name='sub' value='删除'/><a href='system.php'>取消</a></form>";
            $dir=$fname;
            if($_POST['sub']){
                delDir($dir);
            }
 
            break;
        case 10:
             //进入                   
            yemian($cur);
            break;
        case 11:
             //编辑
            $fname=$_GET['fname'];
            $content=file_get_contents($fname);
 
            echo '<center><h1>编辑文件</h1><hr/>';
            echo '<form method="post" action="#">'."文件名:"."<input type='text'name='filename'value=$fname /><br/>";
            echo '<textarea name="content" rows="40" cols="160" >'.$content.'</textarea><br/>';
            echo "<input type='submit' name='sub' value='保存文件'/><input type='reset' value='重置'/><a href='system.php'>取消</a></center></form>";
            if($fname!="" && $content!=""){
                $name=$_POST['filename'];
                $con=$_POST['content'];
                file_put_contents($name,$con);       
            }
            break;
        case 12:
            //下载
            $fname=$_GET['fname'];
            download($fname);
            break;
        default:
            echo "系统有问题,请和管理员联系!";
            break;
    }
?>
<?php
    //定义一个自动生成页面的函数
    function yemian($cur){
        echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
            <head>
            <meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
            <title>File System</title>
            <style type="text/css">
            TD { FONT-SIZE: 10pt; COLOR: #0033cc; FONT-FAMILY: 新宋体;}
            A { FONT-WEIGHT: normal; COLOR: #000033; FONT-FAMILY: 新宋体; TEXT-DECORATION: none;}
            A:hover { COLOR: #ff0000; TEXT-DECORATION: underline;}
            p {MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; LINE-HEIGHT: 150%}
            </style>
            </head>
            <body bgColor=#ccffcc text="#000033">';
        echo '<center>
                <h1>文件目录操作系统</h1>
                <p/><hr/>
                </center>
                <a href="system.php?action=1">上传文件</a>||<a href="system.php?action=2">新建文件夹</a>||<a href="system.php?action=3">创建文件</a>||<a href="system.php?action=4&fname='.$cur.'">上级目录</a>||<a href="system.php?action=5">开始目录</a>||<a href="system.php?action=6">文档根目录</a>
                <hr/>
                <table width=100%  border="0.5">
                <tr height="20" bgcolor="#b0c4de">
                <th width="10%" align="center">类型</th>
            <th width="20%" align="center">名称</th>
            <th width="10%" align="center">大小</th>
            <th width="20%" align="center">修改时间</th>
            <th width="40%" align="center">操作</th>
            </tr>';
 
                $str=ssdir($cur); //获取当前目前下的所有目录及文件的信息
                $arrfile=$arrdir=array(); //定义两个空的数组
                $m=$n=0;
                $arr=explode("##",$str);
                $arrdir=explode("|",$arr[0]);
                $arrfile=explode("|",$arr[1]);
                if($arrdir[0]!=""){
                foreach($arrdir as $ad){
                    echo "<tr>";
                    $m++;
                    if($m%2==0){
                    echo "<tr bgcolor='#b0c4de'>";
                    }
                    $a=explode(",",$ad);
                    foreach($a as $v){
                        echo "<td>$v</td>";                       
                    }
                    $ftype=$a[0];
                    $fname=$a[1];
                    $fsize=$a[2];
                    $fmtime=$a[3];
 
                    echo '<td><a href="system.php?action=7&fname='.$fname.'&ftype='.$ftype.'&fsize='.$fsize.'&fmtime='.$fmtime.'">复制</a>/<a href="system.php?action=8&fname='.$fname.'&ftype='.$ftype.'&fsize='.$fsize.'&fmtime='.$fmtime.'">重命名</a>/<a href="system.php?action=9&fname='.$fname.'&ftype='.$ftype.'&fsize='.$fsize.'&fmtime='.$fmtime.'">删除</a>/<a href="system.php?action=10&fname='.$cur."/".$fname.'&ftype='.$ftype.'&fsize='.$fsize.'&fmtime='.$fmtime.'">进入</a></td>';
                    echo "</tr>";       
                    echo "</tr>";
                    }
                }
                if($arrfile[0]!=""){
                foreach($arrfile as $af){
                    echo "<tr>";
                    $n++;
                    if($n%2==0){
                    echo "<tr bgcolor='#b0c4de'>";
                    }
                    $b=explode(",",$af);
                    foreach($b as $v){
                        echo "<td >$v</td>";
                    }
                    $ftype=$b[0];
                    $fname=$b[1];
                    $fsize=$b[2];
                    $fmtime=$b[3];
                    echo '<td><a href="system.php?action=7&fname='.$fname.'&ftype='.$ftype.'&fsize='.$fsize.'&fmtime='.$fmtime.'">复制</a>/<a href="system.php?action=8&fname='.$fname.'&ftype='.$ftype.'&fsize='.$fsize.'&fmtime='.$fmtime.'">重命名</a>/<a href="system.php?action=9&fname='.$fname.'&ftype='.$ftype.'&fsize='.$fsize.'&fmtime='.$fmtime.'">删除</a>/<a href="system.php?action=11&fname='.$fname.'&ftype='.$ftype.'&fsize='.$fsize.'&fmtime='.$fmtime.'">编辑</a>/<a href="system.php?action=12&fname='.$fname.'&ftype='.$ftype.'">下载</a></td>';
                    echo "</tr>";
                    echo "</tr>";
                    }
                }
            echo '</table>';
            echo '<br/>';
            echo '</body></html>';
            $num=$m+$n;
            $t=size(disk_total_space($cur));
            $k=size(disk_free_space($cur));
            $y=size((disk_total_space($cur)-disk_free_space($cur)));
            echo "<span style='color:#005500;fontsize:13px;' >本目录下共有文件 {$num} 个,其中目录{$m} 个,文件{$n}个。所在的总分区大小:{$t}  已用 {$y} 可用 {$k}</span><hr/>";
            echo '<center>作者:刘政 版本:1.0 编写时间:2011-9-17</center>';
        }
    //定义一个遍历当前目录的函数
    function ssdir($dir){
        if(file_exists($dir)){
            if($op=opendir($dir)){
                while($ndir=readdir($op)){
                    if($ndir!="." && $ndir!=".."){
                        $sdir=$dir."/".$ndir;
                        //文件的数据
                        if(is_file($sdir)){
                            $type=filetype($sdir);
                            $name=$ndir;
                            $size=size(filesize($sdir));
                            $mtime=date("Y-m-d H:i:s",filemtime($sdir)+8*3600);
                            $arrfile.=$type.",".$name.",".$size.",".$mtime."|";
 
                        }
                        //目录的数据
                        if(is_dir($sdir)){
                            //ssdir($sdir);  //递归目录等一会再实现功能 ,现在目录大小还是0,                                                   
                            $size=size(dirsize($sdir));
                            $type=filetype($sdir);
                            $name=$ndir;                           
                            $mtime=date("Y-m-d H:i:s",filemtime($sdir)+8*3600);
                            $arrdir.=$type.",".$name.",".$size.",".$mtime."|";
 
                        }
                    }
                }
            }
            closedir($op);
            $str=rtrim($arrdir,"|")."##".rtrim($arrfile,"|");
            return $str;
 
        }
    }
    //定义一个统计文件大小的单位的函数
    function size($size){
        $dw="";
        $n="";
        if($size>pow(1024,3)){
            $dw="G";
            $n=round($size/pow(1024,3),2);
        }else if($size>pow(1024,2)){
            $dw="M";
            $n=round($size/pow(1024,2),2);
        }else if($size>pow(1024,1)){
            $dw="KB";
            $n=round($size/pow(1024,1),2);
        }else{
            $dw="byte";
            $n=$size;
        }
        return $n.$dw;
    }
    //统计文件目录大小的函数
        function dirsize($dirname)
        {   
            $size=0;
            if(file_exists($dirname))
            {
                if(is_dir($dirname))
                {
                    $dir=opendir($dirname);
                    while($filename=readdir($dir))
                    {
                        if($filename!='.' && $filename!='..')
                        {
                            $file=$dirname.'/'.$filename;
                            if(is_dir($file))
                            {
                                $size+=dirsize($file);  
                            }
                            else
                            {
                                $size+=filesize($file);  
                            }
                        }
                    }
                }
            }
            return $size;
        }
    //上传文件的函数
//    $fum=$_FILES['images']['error']; //获取错误信息
//    $ftype=$_FILES['images']['type']; //获取上传文件的类型     
//    $fhz=$_FILES['images']['name'];  //获取上传文件的后缀名
//    $fsize=$_FILES['images']['size']; //获取文件的大小
//    $ftmpname=$_FILES['images']['tmp_name']; //获取文件上传时的临时文件所在位置
    function upload($fnum,$ftype,$fhz,$fsize,$ftmpname){
        if($fnum>0){
        switch($fnum){
            case 1:
                echo "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值";
                break;
            case 2:
                echo "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值";
                break;
            case 3:
                echo "文件只有部分被上传";
                break;
            case 4:
                echo "没有文件被上传";
                break;
            default:
                echo "请检查文件是否已被选择";
                break;
        }
        exit();
    }
    //文件类型的判断
    //foreach($ftype as $type){
        //var_dump($ftype);
        list($dx)=explode("/",$ftype);
        if($dx!=null && $dx!="image" && $dx!="text" && $dx!="application"){
            echo "只能上传图片、文件和压缩包!";
            exit();
        }
    //}   
    //文件的后缀名的判断
    //foreach($fhz as $hz){
        $arrhz=explode(".",$fhz);
        $ahz=array_pop($arrhz);
        $allow=array("jpg","png","gif","bmp","jpeg","BMP");
        if(!in_array($ahz,$allow)){
            echo "请输入jpg,png,gif,bmp,jpeg格式";
            exit();
        }       
    //}   
    //文件的大小的判断
    //var_dump($fsize);
    //foreach($fsize as $size){
        if($fsize >500000){
            echo "图片大小不能超过500KB!";
            exit();
        }
    //}
    //上传文件 从临时文件夹中提取文件到指定位置
    //foreach ($ftmpname as $tmp){
    //    echo $tmp;
        if(is_uploaded_file($ftmpname)){
            //产生一个随机的文件图片名
            $filename=date("YmdHis").rand(100,199).".".$ahz;
            if(move_uploaded_file($ftmpname,$cur.$filename)){
                echo "上传成功!";
            }else{
                echo "上传失败!";
            }
        }
    }
    //}
    //删除文件函数
    function delDir($dir){
        if(is_file($dir)){
            unlink($dir);
            echo "删除".$dir."成功!"."<br/>";
            exit();
        }
        if(file_exists($dir)){
            if($op=@opendir($dir)){
            while($dirfile=readdir($op)){
                if($dirfile!="."  && $dirfile!=".."){
                    $newdir=$dir."/".$dirfile;
                    if(is_dir($newdir))
                        delDir($newdir);
 
                    if(is_file($newdir))
                        unlink($newdir);
                    echo "删除".$newdir."成功!"."<br/>";
                }
            }
            closedir($op);
            rmdir($dir);
            echo "删除".$dir."成功!"."<br/>";
            }
        }       
    }
    //拷贝文件或目录
    function copyDir($subdir,$newdir){
        if(!file_exists($newdir)){
            mkdir($newdir);                //注意目录不能自动创建,只能手动创建
        }
        if(file_exists($subdir)){
            if($op=@opendir($subdir)){
                while($dir=readdir($op)){
                    if($dir!="." && $dir!=".."){
                        $sdf=$subdir."/".$dir;  //定义原目录的路径
                        $ndf=$newdir."/".$dir;  //定义新目录的路径
                        if(is_dir($sdf))
                            copyDir($sdf,$ndf);
                        if(is_file($sdf))
                            copy($sdf,$ndf);
                    }
                }
            }
            closedir($op);
        }
    }
    //重命名
    function rname($name,$newname){
        if(file_exists($name)){
            rename($name,$newname);
            echo "改名成功!";
        }
    }
    //下载
    function download($fname){
        //header("Content-type: application/$ftype");
        header("Content-Disposition: attachment;filename=$fname");
        //header("Content-Length:.filesize($fsize)");
        readfile($fname);
    }
    //编辑
    function edit($fname,$fcontent){
        file_get_contents($fname);
        file_put_contents($fname,$fcontent);
    }
?>
posted @ 2012-05-28 12:53  holyes  阅读(230)  评论(0编辑  收藏  举报