Home > Backend Development > PHP Tutorial > Comprehensive case of PHP file programming-implementation of file upload_PHP tutorial

Comprehensive case of PHP file programming-implementation of file upload_PHP tutorial

WBOY
Release: 2016-07-21 15:01:02
Original
1368 people have browsed it

PHP file upload
1. upload.php

Copy code The code is as follows:

< ;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


ddd





/ td> style="width:300px;">
                                                                 
                                                                value="upload">
>


2. uploadProcess.php



Copy code
The code is as follows:

//Receive
$username=$_POST['username'];
$fileintro=$_POST['fileintro'];

//echo $username.$fileintro;
//Get file information
/* echo "
";<br> print_r($_FILES);<br> echo "
" ;
*/
//Get the size of the file
$file_size=$_FILES['myfile']['size'];
if($file_size>2*1024*1024){
echo "";
exit();
}
// Get the 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();
}

//Determine whether the upload is OK
if(is_uploaded_file($_FILES['myfile'][ 'tmp_name'])) {
// The uploaded files are transferred to the directory you want
$ upload_file = $ _ files ['myfile'] ['tmp_name'];

// To prevent image overwriting problems, create a folder for each user                                      {
              mkdir ($user_path); Problems with the same user 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;
//Chinese to be transcoded
if(move_uploaded_file($upload_file,iconv("utf- 8 "," gb2312 "," $ move_to_file "))) {
echo $ _files ['myfile'] ['name']. ";
}
}else{
echo "Upload failed";
}
?>



3. Encapsulation:



Copy code
The code is as follows:

    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 "";
                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']; _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 to be transcoded
if(move_uploaded_file($this->upload_t mp_path,iconv("utf- 8","gb2312","$move_to_file"))){
                                                                                                                                                                                                  Failed";
                           
} Else {
Echo "upload failed";
}
}

// Get the expansion of the file
Public Function GetFileext ($ FILENAME) {
$fileExt=pathinfo($filename);
            return $fileExt["extension"];
                                                              



http://www.bkjia.com/PHPjc/328021.html

www.bkjia.com
true

http: //www.bkjia.com/PHPjc/328021.html

PHP file upload 1. upload.php Copy the code as follows: !DOCTYPE HTML PUBLIC "-//W3C// DTD HTML 4.01 Transitional//EN" html head titleddd/title meta http-equiv="content-type" conte...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template