PHP File Programming Synthesis Case Implementation of file upload

  • 2020-07-21 07:13:02
  • OfStack

PHP file upload
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>
        <!-- Be careful with file uploads :1 , want to have enctyp . 2 , method="post"-->
    <form enctype="multipart/form-data" action="uploadProcess.php" method="post" >
        <table>
            <tr><td> Please fill in your user name </td><td><input type="text" name="username"></td></tr>
            <tr><td> Please give a brief introduction to the document </td><td><textarea rows="7" cols="50" name="fileintro" style="width:300px;"></textarea></td></tr>
            <tr><td> Please upload your file </td><td><input type="file" name="myfile"></td></tr>
            <tr><td colspan="2"><input type="submit" value=" upload "><td></tr>
        </table>
    </form>
  </body>
</html>

2, uploadProcess php

<?php
    // receive 
    $username=$_POST['username'];
    $fileintro=$_POST['fileintro'];

    //echo $username.$fileintro;
    // Get file information 
/*    echo "<pre>";
    print_r($_FILES);
    echo "</pre>";
*/    
    // Gets the size of the file 
    $file_size=$_FILES['myfile']['size'];
    if($file_size>2*1024*1024){
        echo "<script type='text/javascript'>window.alert(' The file cannot be greater than 2M')</script>";
        exit();
    }
    // Get file type 
    $file_type=$_FILES['myfile']['type'];
    if($file_type!="image/jpeg" && $file_type!="image/pjpeg"){
        echo " The file type can only be  jpg  format ";
        exit();
    }

    // Decide whether to upload or not OK
    if(is_uploaded_file($_FILES['myfile']['tmp_name'])){
        // Get the uploaded file   Transfer to the directory you wish 
        $upload_file=$_FILES['myfile']['tmp_name'];

        // Prevent image overwrite issues and create for each user 1 A folder     
        $user_path=$_SERVER['DOCUMENT_ROOT']."/file/up/".$username;
        if(!file_exists($user_path)){
            mkdir ($user_path);
        }
        //$move_to_file=$user_path."/".$_FILES['myfile']['name'];
        // Prevent users from uploading the same username problem 
        $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;
        // Chinese should be transcoded 
        if(move_uploaded_file($upload_file,iconv("utf-8","gb2312","$move_to_file"))){
            echo $_FILES['myfile']['name']." Uploaded successfully ";
        }else{
            echo " Upload failed ";
        }
    }else{
        echo " Upload failed ";
    }
?>

3. Packaging:

<?php
    class Upload{
        public $upload_name; // Upload file name 
        public $upload_tmp_path; // Upload the file to the server temp The path 
        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){
            // Determine file size 
            if($this->file_size>2*1024*1024){
                echo "<script type='text/javascript'>window.alert(' The file cannot be greater than 2M')</script>";
                exit();
            }
            // Get file type 
/*            if($this->file_type!="image/jpeg" && $this->file_type!="image/pjpeg"){
                echo " The file type can only be  jpg  format ";
                exit();
            }
*/            // Gets the extension of the file 
            $file_type=$this->getFileExt($this->upload_name);
            if(!in_array($file_type,$this->allow_file_type)){
                echo " Upload file type format error ";
                exit();
            }            
            // Decide whether to upload or not OK
            if(is_uploaded_file($this->upload_tmp_path)){

                // Prevent image overwrite issues and create for each user 1 A folder     
                $user_path=$this->file_save_path.$username;
                if(!file_exists($user_path)){
                    mkdir ($user_path);
                }
                //$move_to_file=$user_path."/".$_FILES['myfile']['name'];
                // Prevent users from uploading the same username problem 
                //$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;
                // Chinese should be transcoded 
                if(move_uploaded_file($this->upload_tmp_path,iconv("utf-8","gb2312","$move_to_file"))){
                    echo $this->upload_name." Uploaded successfully ";
                }else{
                    echo " Upload failed ";
                }
            }else{
                echo " Upload failed ";
            }
        }

        // Gets the extension of the file 
        public function getFileExt($filename){
            $fileExt=pathinfo($filename);
            return $fileExt["extension"];
        }
    }
?>

Related articles: