加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php实现WEB在线文件管理器

发布时间:2020-12-13 20:56:27 所属栏目:PHP教程 来源:网络整理
导读:1、效果图 功能包括: 文件夹:创建文件夹、查看文件夹、复制文件夹、剪切文件夹、删除文件夹、重命名文件夹 文件:查看文件、创建文件、编辑文件、复制文件、剪切文件、删除文件、重命名文件、下载文件、上传文件 2、目录结构预览 ? ? 3、目录操作函数 dir.

1、效果图

功能包括:

文件夹:创建文件夹、查看文件夹、复制文件夹、剪切文件夹、删除文件夹、重命名文件夹

文件:查看文件、创建文件、编辑文件、复制文件、剪切文件、删除文件、重命名文件、下载文件、上传文件

2、目录结构预览

?

?

3、目录操作函数 dir.func.php

<?php 

// 目录操作函数集合

//打开指定目录
/**
 * 遍历目录函数,只读取目录中的最外层的内容
 * @param string $path
 * @return array
 */
function readDirectory($path) {
    $handle = opendir (  );
    while ( ($item = readdir ( $handle )) !== false ) {
        .和..这2个特殊目录
        if ($item != "." && $item != "..") {
            if (is_file ( $path . "/" . $item )) {
                $arr ['file'] [] = ;
            }
            is_dir ( $arr ['dir'] [] = ;
            }
        
        }
    }
    closedir ( $handlereturn $arr;
}
$path="file";
//print_r(readDirectory($path));

/**
 * 得到文件夹大小
 * @param string $path
 * @return int 
 function dirSize(){
    $sum=0;
    global $sum;
    $handle=opendir();
    while(($item=readdir($handle))!==){
        if($item!="."&&$item!=".."){
            if(is_file($path."/".)){
                $sum+=filesize();
            }
            is_dir($func=__FUNCTION__;
                $func();
            }
        }
        
    }
    closedir($path="file";
//echo dirSize($path);

function createFolder($dirname){
    检测文件夹名称的合法性
    if(checkFilename(basename())){
        当前目录下是否存在同名文件夹名称
        if(!file_exists()){
            mkdir($dirname,0777,true$mes="文件夹创建成功";
            }else{
                $mes="文件夹创建失败";
            }
        }{
            $mes="存在相同文件夹名称";
        }
    }{
        $mes="非法文件夹名称";
    }
    $mes*
 * 重命名文件夹
 * @param string $oldname
 * @param string $newname
 * @return string
 function renameFolder($oldname,$newname检测当前目录下是否存在同名文件夹名称
        rename($mes="重命名成功"$mes="重命名失败"$mes="存在同名文件夹";
}

function copyFolder($src,1)">$dstecho $src,"---",$dst."----";
    )){
        $dst,1)">);
    }
    $src$src."/".)){
                copy($item,1)">$dst."/".);
            }
        }
    }
    return "复制成功";
    
}

*
 * 剪切文件夹
 * @param string $src
 * @param string $dst
 * @return string
 function cutFolder(
    $dst."/".))){
                ))){
                    $mes="剪切成功";
                }{
                    $mes="剪切失败";
                }
            }$mes="不是一个文件夹"$mes="目标文件夹不存在"*
 * 删除文件夹
 * @param string $path
 * @return string
 function delFolder(unlink(rmdir(return "文件夹删除成功";
}

?

4、文件操作函数 file.func.php

<? 文件操作函数集合

//Bytes/Kb/MB/GB/TB/EB*
 * 转换字节大小
 * @param number $size
 * @return number
 function transByte($size$arr = array ("B","KB","MB","GB","TB","EB" );
    $i = 0while ( $size >= 1024 ) {
        $size /= 1024;
        $i ++return round ( $size,2 ) . $arr [$i];
}

*
 * 创建文件
 * @param string $filename
 * @return string
 function createFile($filename) {
    file/1.txt
    //验证文件名的合法性,是否包含/,*,<>,?,|
    $pattern = "/[/,*,?|]/"if (! preg_match ( $pattern,1)">basename (  ) )) {
        检测当前目录下是否存在同名文件
        file_exists (  )) {
            通过touch($filename)来创建
            touch (  )) {
                return "文件创建成功";
            }  {
                return "文件创建失败";
            }
        }  {
            return "文件已存在,请重命名后创建";
        }
    }  {
        return "非法文件名";
    }
}

*
 * 重命名文件
 * @param string $oldname
 * @param string $newname
 * @return string
 function renameFile(){
    echo $oldname,$newname;
//验证文件名是否合法
    if(checkFilename(检测当前目录下是否存在同名文件
        $path=dirname($oldname);
        进行重命名
            return "重命名成功"{
                return "重命名失败"{
            return "存在同名文件,请重新命名"{
        ;
    }
    
}

*
 *检测文件名是否合法
 * @param string $filename
 * @return boolean
 function checkFilename( )) {
        ;
    }*
 * 删除文件
 * @param string $filename
 * @return string
 function delFile()){
        $mes="文件删除成功"$mes="文件删除失败"*
 * 下载文件操作
 * @param string $filename
 function downFile(header("content-disposition:attachment;filename=".));
    header("content-length:".readfile();
}

*
 * 复制文件
 * @param string $filename
 * @param string $dstname
 * @return string
 function copyFile($filename,1)">$dstname$dstname."/".))){
            ))){
                $mes="文件复制成功"$mes="文件复制失败"$mes="存在同名文件"$mes="目标目录不存在"function cutFile($mes="文件剪切成功"$mes="文件剪切失败"*
 * 上传文件
 * @param array $fileInfo
 * @param string $path
 * @param array $allowExt
 * @param int $maxSize
 * @return string
 function uploadFile($fileInfo,1)">$path,1)">$allowExt=array("gif","jpeg","jpg","png","txt"),1)">$maxSize=10485760判断错误号
    $fileInfo['error']==UPLOAD_ERR_OK){
        文件是否是通过HTTP POST方式上传上来的
        is_uploaded_file($fileInfo['tmp_name'])){
            上传文件的文件名,只允许上传jpeg|jpg、png、gif、txt的文件
            //$allowExt=array("gif","txt");
            $ext=getExt($fileInfo['name']);
            $uniqid=getUniqidName();
            $destination=$path."/".pathinfo($fileInfo['name'],PATHINFO_FILENAME)."_".$uniqid.".".$ext;
            in_array($ext,1)">$allowExt$fileInfo['size']<=$maxSize){
                    move_uploaded_file($fileInfo['tmp_name'],1)">$destination)){
                        $mes="文件上传成功";
                    }{
                        $mes="文件移动失败";
                    }
                }$mes="文件过大"$mes="非法文件类型"$mes="文件不是通过HTTP POST方式上传上来的"switch($fileInfo['error']){
            case 1:
                $mes="超过了配置文件的大小";
                breakcase 2:
                $mes="超过了表单允许接收数据的大小"case 3:
                $mes="文件部分被上传"case 4:
                $mes="没有文件被上传";
        }
    }
    
    ;
    
}

?

5、公共函数 common.func.php

<? 公共函数集合

*
 * 提示操作信息的,并且跳转
 * @param string $mes
 * @param string $url
 function alertMes($mes,1)">$urlecho "<script type='text/javascript'>alert('{$mes}');location.href='{$url}';</script>"*
 * 截取文件扩展名
 * @param string $filename
 * @return string
 function getExt(strtolower(PATHINFO_EXTENSION));
}

*
 * 产生唯一名称
 * @param int $length
 * @return string
 function getUniqidName($length=10substr(md5(uniqid(microtime(true),1)">true)),1)">$length);
}

?

6、主文件 index.php

<?php 
require_once 'dir.func.php';
require_once 'file.func.php'require_once 'common.func.php';
$path="file"$path=$_REQUEST['path']?$_REQUEST['path']:$act=$_REQUEST['act'];
$filename=$_REQUEST['filename'$dirname=$_REQUEST['dirname'$info=readDirectory();
if(!$infoecho "<script>alert('没有文件或目录!!!');location.href='index.php';</script>"print_r($info);
$redirect="index.php?path={$path}"$act=="创建文件"创建文件
//    echo $path,"--";
//    echo $filename;
    $mes=createFile();
    alertMes($redirect);
}elseif($act=="showContent"查看文件内容
        $content=file_get_contents(echo "<textarea readonly='readonly' cols='100' rows='10'>{$content}</textarea>";
        //高亮显示PHP代码
        //高亮显示字符串中的PHP代码
        strlen($content$newContent=highlight_string($content,1)">高亮显示文件中的PHP代码
        //highlight_file($filename);
        $str=<<<EOF
        <table width='100%' bgcolor='pink' cellpadding='5' cellspacing="0" >
            <tr>
                <td>{$newContent}</td>
            </tr>
        </table>
EOF;
        echo $str{
        alertMes("文件没有内容,请编辑再查看!",1)">);
    }
}$act=="editContent"修改文件内容
    //echo "编辑文件";
    echo $content;
    EOF
    <form action='index.php?act=doEdit' method='post'> 
        <textarea name='content' cols='190' rows='10'>{$content}</textarea><br/>
        <input type='hidden' name='filename' value='{$filename}'/>
        <input type="hidden" name="path" value="{$path}" />
        <input type="submit" value="修改文件内容"/>
    </form>
EOF;
    ;
}$act=="doEdit"修改文件内容的操作
    $content=$_REQUEST['content'];
    echo $content;
    file_put_contents($mes="文件修改成功"$mes="文件修改失败";
    }
    alertMes($act=="renameFile"完成重命名
    EOF
    <form action="index.php?act=doRename" method="post"> 
    请填写新文件名:<input type="text" name="newname" placeholder="重命名"/>
    <input type='hidden' name='filename' value='{$filename}' />
    <input type="submit" value="重命名"/>
    </form>
EOF;
$act=="doRename"实现重命名的操做
    $newname=$_REQUEST['newname'];
    $mes=renameFile($act=="delFile"$mes=delFile($act=="downFile"完成下载的操作
    $mes=downFile($act=="创建文件夹"$mes=createFolder($act=="renameFolder"EOF
            <form action="index.php?act=doRenameFolder" method="post"> 
    请填写新文件夹名称:<input type="text" name="newname" placeholder="重命名"/>
    <input type="hidden" name="path" value="{$path}" />
    <input type='hidden' name='dirname' value='{$dirname}' />
    <input type="submit" value="重命名"/>
    </form>$act=="doRenameFolder"echo $newname,"-",$dirname,$path;
    $mes=renameFolder($act=="copyFolder"){
        EOF
    <form action="index.php?act=doCopyFolder" method="post"> 
    将文件夹复制到:<input type="text" name="dstname" placeholder="将文件夹复制到"/>
    <input type="hidden" name="path" value="{$path}" />
    <input type='hidden' name='dirname' value='{$dirname}' />
    <input type="submit" value="复制文件夹"/>
    </form>$act=="doCopyFolder"$dstname=$_REQUEST['dstname'$mes=copyFolder());
    alertMes($act=="cutFolder"){
            EOF
    <form action="index.php?act=doCutFolder" method="post"> 
    将文件夹剪切到:<input type="text" name="dstname" placeholder="将文件剪切到"/>
    <input type="hidden" name="path" value="{$path}" />
    <input type='hidden' name='dirname' value='{$dirname}' />
    <input type="submit" value="剪切文件夹"/>
    </form>$act=="doCutFolder"echo "文件夹被剪切了";
    $mes=cutFolder($act=="delFolder"完成删除文件夹的操作
    //echo "文件夹被删除了";
    $mes=delFolder($act=="copyFile"){
                EOF
    <form action="index.php?act=doCopyFile" method="post"> 
    将文件复制到:<input type="text" name="dstname" placeholder="将文件复制到"/>
    <input type="hidden" name="path" value="{$path}" />
    <input type='hidden' name='filename' value='{$filename}' />
    <input type="submit" value="复制文件"/>
    </form>$act=="doCopyFile"$mes=copyFile($act=="cutFile"EOF
    <form action="index.php?act=doCutFile" method="post"> 
    将文件剪切到:<input type="text" name="dstname" placeholder="将文件剪切到"/>
    <input type="hidden" name="path" value="{$path}" />
    <input type='hidden' name='filename' value='{$filename}' />
    <input type="submit" value="剪切文件"/>
    </form>$act=="doCutFile"$mes=cutFile($act=="上传文件"print_r($_FILES);
    $fileInfo=$_FILES['myFile'$mes=uploadFile();
}
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
<link rel="stylesheet" href="cikonss.css" />
<script src="jquery-ui/js/jquery-1.10.2.js"></script>
<script src="jquery-ui/js/jquery-ui-1.10.4.custom.js"></script>
<script src="jquery-ui/js/jquery-ui-1.10.4.custom.min.js"></script>
<link rel="stylesheet" href="jquery-ui/css/ui-lightness/jquery-ui-1.10.4.custom.css"  type="text/css"/>
<style type="text/css">
    body,p,div,ul,ol,table,1)">dl,dd,1)">dt{
        margin:0;
        padding: 0;
    }
    a{
        text-decoration: none;
    }
    ul,1)">li{
        list-style: none;
        float: left;
    }
    #top{
        width:100%;
        height:48px;
        margin:0 auto;
        background: E2E2E2;
    }
    navi a{
        display: block;
        width:48px;
        height: 48px;
    }
    main{
        margin:0 auto;
        border:2px solid ABCDEF;
    }
    .small{
        width:25px;
        height:25px;
        border:0;
}
</style>
<script type="text/javascript">
    function show(dis){
    document.getElementById(dis).style.display="block"function delFile(filename,1)">path){
        if(window.confirm("您确定要删除嘛?删除之后无法恢复哟!!!")){
                location.href="index.php?act=delFile&filename="+filename+"&path="+path;
        }
    }
    function delFolder(dirname,1)">)){
            location.href="index.php?act=delFolder&dirname="+dirname+"&path="+function showDetail(t,1)">filename){
        $("#showImg").attr("src",1)">filename);
        $("#showDetail").dialog({
              height:"auto",1)">
              width: "auto",1)">
              position: {my: "center",at: "center",collision:"fit"},1)">
              modal:false,1)">是否模式对话框
              draggable:true,1)">是否允许拖拽
              resizable:是否允许拖动
              title:t,1)">对话框标题
              show:"slide",1)">
              hide:"explode"
        });
        }
    function goBack($back){
        location.href="index.php?path="+;
    }
</script>
</head>

<body>
<div id="showDetail"  style="display:none"><img src="" id="showImg" alt=""/></div>
<h1>在线文件管理器</h1>
<div id="top">
    <ul id="navi">
        <li><a href="index.php" title="主目录"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-home"></span></span></a></li>
        <li><a href="#"  onclick="show('createFile')" title="新建文件" ><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon-file"></span></span></a></li>
        <li><a href="#"  onclick="show('createFolder')" title="新建文件夹"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon-folder"></span></span></a></li>
        <li><a href="#" onclick="show('uploadFile')"title="上传文件"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon-upload"></span></span></a></li>
        <?php 
        $back=($path=="file")?"file":);
        ?>
        <li><a href="#" title="返回上级目录" onclick="goBack('<?php echo $back;?>')"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon-arrowLeft"></span></span></a></li>
    </ul>
</div>
<form action="index.php" method="post" enctype="multipart/form-data">
<table width="100%" border="1" cellpadding="5" cellspacing="0" bgcolor="#ABCDEF" align="center">
<tr id="createFolder"  style="display:none;">
        <td>请输入文件夹名称</td>
        <td >
            <input type="text" name="dirname" />
            <input type="hidden" name="path"  value="<?php echo $path;?>"/>
            <input type="submit"  name="act"  value="创建文件夹"/>
        </td>
</tr>
    <tr id="createFile"  style="display:none;">
        <td>请输入文件名称</td>
        <td >
                <input type="text"  name="filename" />
                <input type="hidden" name="path" value="<?php echo $path;?>"/>
                <input type="submit"  name="act" value="创建文件" />    
        </td>
    </tr>
    <tr id="uploadFile" style="display:none;">
        <td >请选择要上传的文件</td>
        <td ><input type="file" name="myFile" />
            <input type="submit" name="act" value="上传文件" />    
        </td>
    </tr>
    <tr>
        <td>编号</td>
        <td>名称</td>
        <td>类型</td>
        <td>大小</td>
        <td>可读</td>
        <td>可写</td>
        <td>可执行</td>
        <td>创建时间</td>
        <td>修改时间</td>
        <td>访问时间</td>
        <td>操作</td>
    </tr>
<?$info['file']){
    $i=1foreach($info['file'] as $val$p=;
?>
    <tr>
        <td><?php $i;?></td>
        <td><?php $val;?></td>
        <td><?php $src=filetype($p)=="file"?"file_ico.png":"folder_ico.png";?><img src="images/<?php echo $src;?>" alt=""  title="文件"/></td>
        <td><?php echo transByte($p));?></td>
        <td><?php is_readable($p)?"correct.png":"error.png";?><img class="small" src="images/<?php echo $src;?>" alt=""/></td>
        <td><?php is_writable(is_executable($src;?>" alt=""/></td>
        <td><?php echo date("Y-m-d H:i:s",1)">filectime($p));?></td>
        <td><?php filemtime(fileatime($p));?></td>
        <td>
        <?php 
        得到文件扩展名
            $ext=end(explode(".",1)">)));
            $imageExt=);
            $imageExt)){
                ?>    
                <a href="#"  onclick="showDetail('<?php echo $val;?>','<?php echo $p;?>')"><img class="small" src="images/show.png"  alt="" title="查看"/></a>|
        <?php 
            }{
        ?>
        <a href="index.php?act=showContent&path=<?php echo $path;?>&filename=<?php echo $p;?>" ><img class="small" src="images/show.png"  alt="" title="查看"/></a>|
        <?php }?>
        <a href="index.php?act=editContent&path=<?php echo $p;?>"><img class="small" src="images/edit.png"  alt="" title="修改"/></a>|
        <a href="index.php?act=renameFile&path=<?php echo class="small" src="images/rename.png"  alt="" title="重命名"/></a>|
        <a href="index.php?act=copyFile&path=<?php echo class="small" src="images/copy.png"  alt="" title="复制"/></a>|
        <a href="index.php?act=cutFile&path=<?php echo class="small" src="images/cut.png"  alt="" title="剪切"/></a>|
        <a href="#"  onclick="delFile('<?php echo $p;?>',1)">$path;?>')"><img class="small" src="images/delete.png"  alt="" title="删除"/></a>|
        <a href="index.php?act=downFile&path=<?php echo class="small"  src="images/download.png"  alt="" title="下载"/></a>
        </td>        
    </tr>
<?php 
$i++;
    }
}



?>

<!-- 读取目录的操作-->
<?$info['dir'$i=$i==null?1:$info['dir'] $src;?>" alt=""  title="文件"/></td>
        <td><?php  $sum=0; echo transByte(dirSize($p));?></td>
        <td>
        <a href="index.php?path=<?php echo class="small" src="images/show.png"  alt="" title="查看"/></a>|
        <a href="index.php?act=renameFolder&path=<?php echo $path;?>&dirname=<?php echo class="small" src="images/rename.png"  alt="" title="重命名"/></a>|
        <a href="index.php?act=copyFolder&path=<?php echo class="small" src="images/copy.png"  alt="" title="复制"/></a>|
        <a href="index.php?act=cutFolder&path=<?php echo class="small" src="images/cut.png"  alt="" title="剪切"/></a>|
        <a href="#"  onclick="delFolder('<?php echo class="small" src="images/delete.png"  alt="" title="删除"/></a>|
        </td>        
    </tr>
<?;
    }
}

?>

    
    
    
</table>
</form>

</body>
</html>

?

缺点:php混编

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读