zoukankan      html  css  js  c++  java
  • Web 在线文件管理器学习笔记与总结(9)下载文件

    ① 普通形式的文件可以使用超链接形式下载

    <a href = '下载文件名'>点击下载</a>

    ② 如果下载图片、html 等类型的文件,使用header() 函数发送网页头信息实现文件下载

    <?php
    $filename = $_GET['filename'];
    header('content-disposition:attachment;filename=emperor_'.basename($filename);
    header('content-length:'.filesize($filename));
    readfile($filename);

    index.php:

    <?php 
    require 'dir.func.php';
    require 'file.func.php';
    require 'common.func.php';
    $path = 'file';
    $info = readDirectory($path);
    
    $act = @$_REQUEST['act'];
    $filename = @$_REQUEST['filename'];
    //跳转变量
    $redirect = "index.php?path={$path}";
    if($act == 'createFile'){
        //创建文件
        $mes = createFile($path.'/'.$filename);
        alertMes($mes,$redirect);
    }else if($act == 'showContent'){
    //查看文件内容
        $content=file_get_contents($filename);
        //echo "<textarea readonly='readonly' cols='100' rows='10'>{$content}</textarea>";
        //高亮显示PHP代码
        //高亮显示字符串中的PHP代码
        if(strlen($content)){
        $newContent=highlight_string($content,true);
        //高亮显示文件中的PHP代码
        //highlight_file($filename);
        $str=<<<EOF
        <table width='100%' bgcolor='pink' cellpadding='5' cellspacing="0" >
            <tr>
                <td>$newContent</td>
            </tr>
        </table>
    EOF;
            echo $str;
        }else{
            alertMes("文件没有内容,请编辑再查看!",$redirect);
        }
    }else if($act == 'editContent'){
        $content = file_get_contents($filename);
        $str=<<<EOF
        <form action='index.php?act=doEdit' method='post'>
        <textarea name='content'  cols='100' rows='10'>$content</textarea></br>
        <input type='hidden' name='filename' value='{$filename}'>
        <input type='submit' value='修改文件内容'>
        </form>
    EOF;
        echo $str; 
    }else if($act == 'doEdit'){
        //修改文件内容
        $content = $_POST['content'];
        if(file_put_contents($filename, $content)){
            $mes = '文件修改成功';
        }else if(!$content){
            $mes = '文件内容被清空';
        }else{
            $mes = '文件修改失败';
        }
        alertMes($mes,$redirect);
    }else if($act == 'renameFile'){
        //重命名文件
        $str = <<<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;
    echo $str;
    }else if($act == 'doRename'){
        //实现重命名操作
        $newname = $_POST['newname'];
        $mes = renameFile($filename,$newname);
        alertMes($mes,$redirect);
    }else if($act == 'delFile'){
        $mes = delFile($filename);
        alertMes($mes,$redirect);
    }else if($act == 'downFile'){
        downFile($filename);
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <link rel="stylesheet" href="cikonss.css" />
    <link rel="stylesheet" href="common.css" />
    <script src="jquery-1.8.3.min.js"></script>
    <script src="jquery-ui-1.11.3/jquery-ui.min.js"></script>
    <link rel="stylesheet" href="jquery-ui-1.11.3/jquery-ui.min.css" />
    </head>
    <body>
    <div id="showDetail" style="display:none"><img src="" alt="" id="showImg"></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 icon-small icon-square"><span class="icon-file"></span></span></a></li>
            <li><a href="#" title="新建文件夹"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-folder"></span></span></a></li>
            <li><a href="#" title="上传文件"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-upload"></span></span></a></li>
            <li><a href="#" title="返回上级目录"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span 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="hidden" name='act' value='createFile'/>
                <input type="submit" 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 align="center">
            <td>编号</td>
            <td>名称</td>
            <td>类型</td>
            <td>大小</td>
            <td>可读</td>
            <td>可写</td>
            <td>可执行</td>
            <td>创建时间</td>
            <td>修改时间</td>
            <td>访问时间</td>
            <td>操作</td>
        </tr>
        <?php 
            if($info['file']){
                $i = 1;
                foreach($info['file'] as $val){
                    $p = $path.'/'.$val;
        ?>
        <tr align="center">
            <td><?php echo $i;?></td>
            <td><?php echo $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(filesize($p));?></td>
            <td><?php $src = is_readable($p)?'correct.png':'error.png';?><img src="images/<?php echo $src;?>" width="32" alt="" title='可读'></td>
            <td><?php $src = is_writeable($p)?'correct.png':'error.png';?><img src="images/<?php echo $src;?>" width="32" alt="" title='可写'></td>
            <td><?php $src = is_executable($p)?'correct.png':'error.png';?><img src="images/<?php echo $src;?>" width="32" alt="" title='可写'></td>
            <td><?php echo date('Y-m-d H:i:s',filectime($p));?></td>
            <td><?php echo date('Y-m-d H:i:s',filemtime($p));?></td>
            <td><?php echo date('Y-m-d H:i:s',fileatime($p));?></td>
            <td>
                <?php 
                    //得到文件扩展名
                    $ext = strtolower(end(explode('.',$val)));
                    $imageExt = array('gif','jpg','png','jpeg');
                    if(in_array($ext, $imageExt)){ ?>
                    <a href="javascript:void(0)" onclick='showDetail("<?php echo $val;?>","<?php echo $p;?>")' title='查看'><img src="images/show.png" width="32" alt=""></a>
                    
                <?php    }else{ ?>
                    <a href="index.php?act=showContent&filename=<?php echo $p;?>" title='查看'><img src="images/show.png" width="32" alt=""></a>
                <?php
                    }
                ?>
                
                <a href="index.php?act=editContent&filename=<?php echo $p;?>" title='修改'><img src="images/edit.png" width="32" alt=""></a>
                <a href="index.php?act=renameFile&filename=<?php echo $p;?>" title='重命名'><img src="images/rename.png" width="32" alt=""></a>
                <a href="" title='复制'><img src="images/copy.png" width="32" alt=""></a>
                <a href="" title='剪切'><img src="images/cut.png" width="32" alt=""></a>
                <a href="javascript:void(0)" onclick="delFile('<?php echo $p;?>')" title='删除'><img src="images/delete.png" width="32" alt=""></a>
                <a href="index.php?act=downFile&filename=<?php echo $p;?>" title='下载'><img src="images/download.png" width="32" alt=""></a>
            </td>
        </tr>
        <?php            
                    $i++;
                }
            }
        ?>
    </table>
    </form>
    <script src='common.js'></script>
    </body>
    </html>
    View Code

    file.func.php 中添加:

    /*
        下载文件
    */    
    function downFile($filename){
        header('content-disposition:attachment;filename=emperor_'.basename($filename));
        header('content-length:'.filesize($filename));
        readfile($filename);
    }

    file.func.php:

    <?php
    /*
        转换字节大小
    */
    function transByte($size){
        $arr = array('B','KB','MB','GB','TB','EB');
        $i = 0;
        while($size > 1024){
            $size /= 1024;
            $i++;
        }
        return round($size,2).' '.$arr[$i];
    }
    
    /*
        创建文件
    */
    /*
    注意:
    createFile函数前面的正则表达式部分使用了 basename 函数,这个函数会过滤掉所有的 / 斜杠。
    这样在文件名中任意输入 / 不会引起报错。
    */    
    function createFile($filename){
        if(checkFilename(end(explode('/',$filename)))){
            //检测当前目录现是否存在同名文件
            if(!file_exists($filename)){
                //通过touch方法创建文件
                if(@touch($filename)){
                    return '文件创建成功';
                }else{
                    return '文件创建失败';
                }
            }else{
                return '文件已存在,请重命名后创建';
            }
        }else{
            return '非法文件名';
        }
    }    
    
    /*
        重命名文件
        @param newname string 重命名之后的文件名
        @param oldname string 重命名之前的文件名
    */    
    function renameFile($oldname,$newname){
        if(checkFilename($newname)){
            $path = dirname($oldname);
            if(!file_exists($path.'/'.$newname)){
                if(rename($oldname,$path.'/'.$newname)){
                    return '重命名成功';
                }else{
                    return '重命名失败';
                }
            }else{
                return '文件名已存在,请重新命名';
            }
        }else{
            return '非法文件名';
        }
    }    
    
    /*
        检测文件名合法性
    */        
    function checkFilename($filename){
        //验证文件名合法性,是否包含特殊字符  / : | * " ? < >
        $pattern = '/[/,*,<,>,?|,\\,:,"]/'; //     * < > | ? : "有效
        if(!preg_match($pattern, $filename)){
            return true;
        }else{
            return false;
        }    
    }    
    
    /*
        删除文件
    */    
    function delFile($filename){
        if(unlink($filename)){
            return '文件成功删除';
        }else{
            return '文件删除失败';
        }
    }
    
    /*
        下载文件
    */    
    function downFile($filename){
        header('content-disposition:attachment;filename=emperor_'.basename($filename));
        header('content-length:'.filesize($filename));
        readfile($filename);
    }
    View Code
  • 相关阅读:
    Power BI for Office 365(八)共享查询
    Power BI for Office 365(七) Power BI站点
    Power BI for Office 365(六)Power Map简介
    Power BI for Office 365(五)Power View第二部分
    Power BI for Office 365(四)Power View第一部分
    Power BI for Office 365(三)Power Pivot
    Power BI for Office 365(二)Power Query
    java 继承、重载、重写与多态
    Android 热修复方案Tinker(一) Application改造
    阿里最新热修复Sophix与QQ超级补丁和Tinker的实现与总结
  • 原文地址:https://www.cnblogs.com/dee0912/p/4392000.html
Copyright © 2011-2022 走看看