zoukankan      html  css  js  c++  java
  • PHP 简单处理--文件下载--文件上传

    文件下载部分

    从 down 目录下下载,先获取目录下所有文件,再为每个文件添加download 信息,主要是文件名,后缀的关系。

    分两部分,down_1.php 部分初始化,点击download 则跳转 down_2.php 处理传送过来的数据(文件名id).

    主要源码如下(主要使用了简单的php 与 “table" 处理:

    部分效果显示:

    down_1.php :

    <?php
    
    // 查找所有文件信息........
    class document{
        public $file_array = array();
        public $foder_array = array();
        public $all_array = array();
    // 查找目录下所有文件
        function fetch($dir){
            $H = opendir($dir);
            while(false !==($_file=readdir($H))){
                if(is_dir($dir."/".$_file)&&$_file !="."&&$_file !=".."&&$_file !="Thumbs.db"){
                    array_push($this -> folder_array,$dir."/".$_file);
                    $this ->fetch($dir."/".$_file);
                }
                else if(is_file($dir."/".$_file)&&$_file !="."&&$_file !=".."&&$_file !="Thumbs.db"){
                    array_push($this -> file_array,$dir."/".$_file);
                }
            }
            closedir($H);
            $this->all_array['foder'] = $this -> foder_array;
            $this ->all_array['file'] = $this ->file_array;
            return $this ->all_array;
        }
    }
    
    
    
    // 实例化,从 down 文件夹中找文件
    $d = new document();
    $f = $d -> fetch("../down");
    $ff = $f['file'];
    $sum = count($ff);
    echo "<table align=center border=1 cellpadding=5>";
    echo "<tr><td align=center>file_name</td><td>download</td></tr>";
    for($i = 0;$i<$sum;$i++)
    {
        echo "<tr><td>$ff[$i]</td><td><a href=index.php?id=$ff[$i]>download</a></td></tr>";
    }
    echo "</table>";
    
    ?>

    down_2.php:

    <?php 
    
    /*  下载文件  */
    // 设置文件查找的限制--条件 
    class dl{
    var $language = "CN";
    var $allow = array(".gif",".jpg",".txt",".exe",".php","png");
    var $serverPath = "./";
    //设置出错语言及出错信息
    var $message_CN = array(
        "0" => "未找到文件",
        "1" => "不允许下载此类型文件"
    );
    var $message_EN = array(
        "0" => "this file does not exists",
        "1" => "can't download this kind of file"
    );
    
    // 构造函数建值
    function __construct($serverPath){
    $this ->serverPath = $serverPath;
    if($this ->language == "CN")
        $this ->message = $this->message_CN;
    else 
        $this ->message = $this ->message_EN;
    }
    
    // 由后缀查找,并设置错误返回方式 
    function get_ext($file){
    $file_ext = substr($file,-4);
    if(!file_exists($this->serverPath."/".$file)){
    $this ->error("0");
    }
    if(!in_array($file_ext,$this->allow)){
    $this ->error("1");
    }
    }
    
    function error($i){
    die ($this->message[$i]);
    }
    
    // 下载文件开始, 需要设置多个 Content-Type  
    function download($filename,$mimeType = "application/octet-stream"){
    $this ->get_ext($filename);
    $fullname = $this->serverPath."/".$filename;
    header("Content-Type : { $mimeType }");
    $filename = "".htmlspecialchars($filename)."";
    $filesize = filesize($fullname);
    header("Content-Disposition : attachment; filename = {$filename} ; charset = gb2312");
    header("Content-length : {$filesize}");
    readfile($fullname);
    exit;
    }
    }
    
    // 实例化..
    $d = new dl("../down");
    $ss = $_GET['id'];
    $pieces = explode("/",$ss);
    $d ->download($pieces[2]);
    
    ?>

    上传文件部分:

       也是通过解析文件名以及后缀,但外加了一个随机构造文件名的方法,以防文件名重复。上传至up 目录下。

    提供了文件浏览功能,其实就是< img src="uploadfile"/> 就解析出来了。

    部分效果图:

    up_1.php 部分:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html>
    <head>
    <script language="javascript" type="text/javascript">
    
    
    </script>
    </head>
    <body>
    <center><h3> Upload files</h3></center>;
    <form name=from1 method="post" action="edit.php" enctype="multipart/form-data">
    <table border=1><tr>
    <td><input type="file" name="file" maxlength="40" value="浏览"/></td>
    <td><input type="submit" name="submit" value="上传"/></td>
    </tr></table>
    </form>
    
    </body>
    </html>

    up_2.php 部分:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html>
    <head>
    <script language="javascript" type="text/javascript">
    
    
    </script>
    </head>
    <?php
    #==============================================
    
    /*  上传文件  */
    
    //  先设置上传目录 及支持类型文件
    $uploaddir = "../up";
    $type = array("jpg","gif","txt","php","jpeg");
    
    // 获取文件后缀 
    function fileext($filename){
    return substr(strchr($filename,"."),1);
    }
    // 为不使文件名重复,使用随机数生成新文件
    function random($length){
    $hash = "CR-";
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWSYZabcdefghijklmnopqrstuvwxyz0123456789";
    $max = strlen($chars) - 1;
    mt_srand(((double)(microtime() * 1000000)));
    for($i = 0; $i<$max; $i++)
        $hash .= $chars[mt_rand(0,$max)];
    
    return $hash;
    }
    
    // 文件上传 处理
    
    $a = strtolower(fileext($_FILES['file']['name']));
    if( !in_array($a,$type))
    {
        $text = implode(",",$type);
        echo "Sorry, you can just upload these kinds of file :".$text."<br/>";
    }
    else 
    {
       $filename = explode(".",$_FILES['file']['name']);
       do{
               $filename[0] = random(10);
               $name = implode(".",$filename);
               $uploadfile = $uploaddir.$name;
               }while(file_exists($uploadfile));
            if( move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile))   
                if($_FILES['file']['size'] > 0)
                {
                    echo "<center> File upload success !  预览 :<img src='$uploadfile'/> </center>";
                    echo "<center> <a href=delete.php>继续上传</a></center>";
                }
                else echo "Failed in uploading !";
    }
    
    ?>
    
    
    </html>

    文件的上传下载涉及了很多格式,有些类型的文件需要特殊处理,===================================

  • 相关阅读:
    冲刺阶段每日站立会议个人博客十二
    冲刺阶段每日站立会议个人博客十一
    针对其他团队建议的反馈
    软件工程概论第五周学习进度条
    冲刺阶段每日站立会议个人博客十
    冲刺阶段每日站立会议个人博客九
    每日站立会议个人博客八
    每日站立会议个人博客七
    软件工程概论第四周学习进度条
    每日站立会议个人博客六
  • 原文地址:https://www.cnblogs.com/imwtr/p/4069140.html
Copyright © 2011-2022 走看看