PHP 檔案編程綜合案例-檔案上傳的實現

來源:互聯網
上載者:User

PHP檔案上傳
1、upload.php
複製代碼 代碼如下:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>ddd</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">   
  </head>    
  <body>
        <!--檔案上傳要注意:1、要有enctyp,2、method="post"-->
    <form enctype="multipart/form-data" action="uploadProcess.php" method="post" >
        <table>
            <tr><td>請填寫使用者名稱</td><td><input type="text" name="username"></td></tr>
            <tr><td>請簡單介紹檔案</td><td><textarea rows="7" cols="50" name="fileintro" style="width:300px;"></textarea></td></tr>
            <tr><td>請上傳你的檔案</td><td><input type="file" name="myfile"></td></tr>
            <tr><td colspan="2"><input type="submit" value="上傳"><td></tr>
        </table>
    </form>
  </body>
</html>

2、uploadProcess.php
複製代碼 代碼如下:<?php
    //接收
    $username=$_POST['username'];
    $fileintro=$_POST['fileintro'];

    //echo $username.$fileintro;
    //擷取檔案資訊
/*    echo "<pre>";
    print_r($_FILES);
    echo "</pre>";
*/   
    //擷取檔案的大小
    $file_size=$_FILES['myfile']['size'];
    if($file_size>2*1024*1024){
        echo "<script type='text/javascript'>window.alert('檔案不能大於2M')</script>";
        exit();
    }
    //擷取檔案類型
    $file_type=$_FILES['myfile']['type'];
    if($file_type!="image/jpeg" && $file_type!="image/pjpeg"){
        echo "檔案類型只能是 jpg 格式";
        exit();
    }

    //判斷上傳是否OK
    if(is_uploaded_file($_FILES['myfile']['tmp_name'])){
        //得到上傳的檔案 轉存到你希望的目錄
        $upload_file=$_FILES['myfile']['tmp_name'];

        //防止圖片覆蓋問題,為每個使用者建立一個檔案夾   
        $user_path=$_SERVER['DOCUMENT_ROOT']."/file/up/".$username;
        if(!file_exists($user_path)){
            mkdir ($user_path);
        }
        //$move_to_file=$user_path."/".$_FILES['myfile']['name'];
        //防止使用者上傳使用者名稱相同的問題
        $file_true_name=$_FILES['myfile']['name'];
        $move_to_file=$user_path."/".time().rand(1,1000).substr($file_true_name,strripos($file_true_name,"."));
        //echo $upload_file.$move_to_file;
        //中文要轉碼
        if(move_uploaded_file($upload_file,iconv("utf-8","gb2312","$move_to_file"))){
            echo $_FILES['myfile']['name']."上傳成功";
        }else{
            echo "上傳失敗";
        }
    }else{
        echo "上傳失敗";
    }
?>

3、封裝:
複製代碼 代碼如下:<?php
    class Upload{
        public $upload_name; //上傳檔案名稱
        public $upload_tmp_path; //上傳檔案儲存到伺服器的temp路徑
        public $file_size;
        public $file_type;
        public $file_save_path;
        function __construct(){
            $this->upload_name=$_FILES['myfile']['name'];
            $this->upload_tmp_path=$_FILES['myfile']['tmp_name'];
            $this->file_size=$_FILES['myfile']['size'];
            $this->file_type=$_FILES['myfile']['type'];
            $this->allow_file_type = array('jpeg','jpg','png','gif','bmp','doc','zip','rar','txt','wps','xlsx','ppt');
            $this->file_save_path=$_SERVER['DOCUMENT_ROOT']."/file/up/";
        }
        public function upload_file($username){
            //判斷檔案大小
            if($this->file_size>2*1024*1024){
                echo "<script type='text/javascript'>window.alert('檔案不能大於2M')</script>";
                exit();
            }
            //擷取檔案類型
/*            if($this->file_type!="image/jpeg" && $this->file_type!="image/pjpeg"){
                echo "檔案類型只能是 jpg 格式";
                exit();
            }
*/            //擷取檔案的副檔名
            $file_type=$this->getFileExt($this->upload_name);
            if(!in_array($file_type,$this->allow_file_type)){
                echo "上傳檔案類型格式錯誤";
                exit();
            }           
            //判斷上傳是否OK
            if(is_uploaded_file($this->upload_tmp_path)){

                //防止圖片覆蓋問題,為每個使用者建立一個檔案夾   
                $user_path=$this->file_save_path.$username;
                if(!file_exists($user_path)){
                    mkdir ($user_path);
                }
                //$move_to_file=$user_path."/".$_FILES['myfile']['name'];
                //防止使用者上傳使用者名稱相同的問題
                //$file_true_name=$_FILES['myfile']['name'];
                $move_to_file=$user_path."/".time().rand(1,1000).substr($this->upload_name,strripos($this->upload_name,"."));
                //echo $upload_file.$move_to_file;
                //中文要轉碼
                if(move_uploaded_file($this->upload_tmp_path,iconv("utf-8","gb2312","$move_to_file"))){
                    echo $this->upload_name."上傳成功";
                }else{
                    echo "上傳失敗";
                }
            }else{
                echo "上傳失敗";
            }
        }

        //擷取檔案的副檔名
        public function getFileExt($filename){
            $fileExt=pathinfo($filename);
            return $fileExt["extension"];
        }
    }
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.