熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> PHP編程 >> 正文

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

2022-06-13   來源: PHP編程 

  PHP文件上傳
uploadphp

復制代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "//WC//DTD HTML Transitional//EN">
<html>
  <head>
    <title>ddd</title>
    <meta httpequiv="contenttype" content="text/html; charset=UTF">   
  </head>    
  <body>
        <!文件上傳要注意:要有enctypmethod="post">
    <form enctype="multipart/formdata" action="uploadProcessphp" method="post" >
        <table>
            <tr><td>請填寫用戶名</td><td><input type="text" name="username"></td></tr>
            <tr><td>請簡單介紹文件</td><td><textarea rows="" cols="" name="fileintro" style="width:px;"></textarea></td></tr>
            <tr><td>請上傳你的文件</td><td><input type="file" name="myfile"></td></tr>
            <tr><td colspan=""><input type="submit" value="上傳"><td></tr>
        </table>
    </form>
  </body>
</html>

  
uploadProcessphp

復制代碼 代碼如下:
<?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>**){
        echo "<script type=text/javascript>windowalert(文件不能大於M)</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()substr($file_true_namestrripos($file_true_name""));
        //echo $upload_file$move_to_file;
        //中文要轉碼
        if(move_uploaded_file($upload_fileiconv("utf""gb""$move_to_file"))){
            echo $_FILES[myfile][name]"上傳成功";
        }else{
            echo "上傳失敗";
        }
    }else{
        echo "上傳失敗";
    }
?>

  
封裝

復制代碼 代碼如下:

  
<?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(jpegjpgpnggifbmpdocziprartxtwpsxlsxppt);
            $this>file_save_path=$_SERVER[DOCUMENT_ROOT]"/file/up/";
        }
        public function upload_file($username){
            //判斷文件大小
            if($this>file_size>**){
                echo "<script type=text/javascript>windowalert(文件不能大於M)</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()substr($this>upload_namestrripos($this>upload_name""));
                //echo $upload_file$move_to_file;
                //中文要轉碼
                if(move_uploaded_file($this>upload_tmp_pathiconv("utf""gb""$move_to_file"))){
                    echo $this>upload_name"上傳成功";
                }else{
                    echo "上傳失敗";
                }
            }else{
                echo "上傳失敗";
            }
        }

        //獲取文件的擴展名
        public function getFileExt($filename){
            $fileExt=pathinfo($filename);
            return $fileExt["extension"];
        }
    }
?>


From:http://tw.wingwit.com/Article/program/PHP/201311/21066.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.