Table of Contents
PHP designed super easy-to-use file upload processing class 1 (37),
Home Backend Development PHP Tutorial PHP design super easy-to-use file upload processing class 1 (37), _PHP tutorial

PHP design super easy-to-use file upload processing class 1 (37), _PHP tutorial

Jul 13, 2016 am 09:52 AM
php one upload deal with good document use kind design

PHP designed super easy-to-use file upload processing class 1 (37),

<?<span>php
    </span><span>class</span><span> FileUpload {
        </span><span>private</span> $filepath;     <span>//</span><span>指定上传文件保存的路径</span>
        <span>private</span> $allowtype=array(<span>'</span><span>gif</span><span>'</span>, <span>'</span><span>jpg</span><span>'</span>, <span>'</span><span>png</span><span>'</span>, <span>'</span><span>jpeg</span><span>'</span>);  <span>//</span><span>充许上传文件的类型</span>
        <span>private</span> $maxsize=<span>1000000</span>;  <span>//</span><span>允上传文件的最大长度 1M</span>
        <span>private</span> $israndname=<span>true</span>;  <span>//</span><span>是否随机重命名, true false不随机,使用原文件名</span>
        <span>private</span> $originName;   <span>//</span><span>源文件名称</span>
        <span>private</span> $tmpFileName;   <span>//</span><span>临时文件名</span>
        <span>private</span> $fileType;  <span>//</span><span>文件类型</span>
        <span>private</span> $fileSize;  <span>//</span><span>文件大小</span>
        <span>private</span> $newFileName; <span>//</span><span>新文件名</span>
        <span>private</span> $errorNum=<span>0</span>;  <span>//</span><span>错误号</span>
        <span>private</span> $errorMess=<span>""</span>; <span>//</span><span>用来提供错误报告



        </span><span>//</span><span>用于对上传文件初使化
        </span><span>//</span><span>1. 指定上传路径, 2,充许的类型, 3,限制大小, 4,是否使用随机文件名称
        </span><span>//</span><span>让用户可以不用按位置传参数,后面参数给值不用将前几个参数也提供值</span>
        function __construct($options=<span>array()){
            </span><span>foreach</span>($options <span>as</span> $key=><span>$val){
                $key</span>=<span>strtolower($key);
                </span><span>//</span><span>查看用户参数中数组的下标是否和成员属性名相同</span>
                <span>if</span>(!in_array($key,get_class_vars(get_class($<span>this</span><span>)))){
                    </span><span>continue</span><span>;
                }

                $</span><span>this</span>-><span>setOption($key, $val);
            }
         
        
        }
    


        </span><span>private</span><span> function getError(){
            $str</span>=<span>"</span><span>上传文件<font color='red'>{$this->originName}</font>时出错:</span><span>"</span><span>;

            </span><span>switch</span>($<span>this</span>-><span>errorNum){
                </span><span>case</span> <span>4</span>: $str .= <span>"</span><span>没有文件被上传</span><span>"</span>; <span>break</span><span>;
                </span><span>case</span> <span>3</span>: $str .= <span>"</span><span>文件只被部分上传</span><span>"</span>; <span>break</span><span>;
                </span><span>case</span> <span>2</span>: $str .= <span>"</span><span>上传文件超过了HTML表单中MAX_FILE_SIZE选项指定的值</span><span>"</span>; <span>break</span><span>;
                </span><span>case</span> <span>1</span>: $str .= <span>"</span><span>上传文件超过了php.ini 中upload_max_filesize选项的值</span><span>"</span>; <span>break</span><span>;
                </span><span>case</span> -<span>1</span>: $str .= <span>"</span><span>末充许的类型</span><span>"</span>; <span>break</span><span>;
                </span><span>case</span> -<span>2</span>: $str .= <span>"</span><span>文件过大,上传文件不能超过{$this->maxSize}个字节</span><span>"</span>; <span>break</span><span>;
                </span><span>case</span> -<span>3</span>: $str .= <span>"</span><span>上传失败</span><span>"</span>; <span>break</span><span>;
                </span><span>case</span> -<span>4</span>: $str .= <span>"</span><span>建立存放上传文件目录失败,请重新指定上传目录</span><span>"</span>; <span>break</span><span>;
                </span><span>case</span> -<span>5</span>: $str .= <span>"</span><span>必须指定上传文件的路径</span><span>"</span>; <span>break</span><span>;

                </span><span>default</span>: $str .=  <span>"</span><span>末知错误</span><span>"</span><span>;
            }

            </span><span>return</span> $str.<span>'</span><span><br></span><span>'</span><span>;
        }
    
        </span><span>//</span><span>用来检查文件上传路径</span>
        <span>private</span><span> function checkFilePath(){
            </span><span>if</span>(empty($<span>this</span>-><span>filepath)) {
                $</span><span>this</span>->setOption(<span>'</span><span>errorNum</span><span>'</span>, -<span>5</span><span>);
                </span><span>return</span> <span>false</span><span>;
            }

            </span><span>if</span>(!file_exists($<span>this</span>->filepath) || !is_writable($<span>this</span>-><span>filepath)){
                </span><span>if</span>(!@mkdir($<span>this</span>->filepath, <span>0755</span><span>)){
                    $</span><span>this</span>->setOption(<span>'</span><span>errorNum</span><span>'</span>, -<span>4</span><span>);
                    </span><span>return</span> <span>false</span><span>;
                }
            }
            </span><span>return</span> <span>true</span><span>;
        }
        </span><span>//</span><span>用来检查文件上传的大小</span>
        <span>private</span><span> function checkFileSize() {
            </span><span>if</span>($<span>this</span>->fileSize > $<span>this</span>-><span>maxsize){
                $</span><span>this</span>->setOPtion(<span>'</span><span>errorNum</span><span>'</span>, <span>'</span><span>-2</span><span>'</span><span>);
                </span><span>return</span> <span>false</span><span>;
            }</span><span>else</span><span>{
                </span><span>return</span> <span>true</span><span>;
            }
        }

        </span><span>//</span><span>用于检查文件上传类型</span>
        <span>private</span><span> function checkFileType() {
            </span><span>if</span>(in_array(strtolower($<span>this</span>->fileType), $<span>this</span>-><span>allowtype)) {
                </span><span>return</span> <span>true</span><span>;
            }</span><span>else</span><span>{
                $</span><span>this</span>->setOption(<span>'</span><span>errorNum</span><span>'</span>, -<span>1</span><span>);
                </span><span>return</span> <span>false</span><span>;
            }
        }
        </span><span>//</span><span>设置上传后的文件名称</span>
        <span>private</span><span> function setNewFileName(){
            </span><span>if</span>($<span>this</span>-><span>israndname){
                $</span><span>this</span>->setOption(<span>'</span><span>newFileName</span><span>'</span>, $<span>this</span>-><span>proRandName());
            } </span><span>else</span><span> {
                $</span><span>this</span>->setOption(<span>'</span><span>newFileName</span><span>'</span>, $<span>this</span>-><span>originName);
            }
        }



        </span><span>//</span><span>设置随机文件名称</span>
        <span>private</span><span> function proRandName(){
            $fileName</span>=date(<span>"</span><span>YmdHis</span><span>"</span>).rand(<span>100</span>,<span>999</span><span>);
            </span><span>return</span> $fileName.<span>'</span><span>.</span><span>'</span>.$<span>this</span>-><span>fileType;
        }
    
        </span><span>private</span><span> function setOption($key, $val){
            $</span><span>this</span>->$key=<span>$val;
        }
        </span><span>//</span><span>用来上传一个文件</span>
<span>        function uploadFile($fileField){
            $</span><span>return</span>=<span>true</span><span>;
            </span><span>//</span><span>检查文件上传路径</span>
            <span>if</span>(!$<span>this</span>-><span>checkFilePath()){
                $</span><span>this</span>->errorMess=$<span>this</span>-><span>getError();
                </span><span>return</span> <span>false</span><span>;
            }

            
            $name</span>=$_FILES[$fileField][<span>'</span><span>name</span><span>'</span><span>];
            $tmp_name</span>=$_FILES[$fileField][<span>'</span><span>tmp_name</span><span>'</span><span>];
            $size</span>=$_FILES[$fileField][<span>'</span><span>size</span><span>'</span><span>];
            $error</span>=$_FILES[$fileField][<span>'</span><span>error</span><span>'</span><span>];

            </span><span>if</span><span>(is_Array($name)){
                $errors</span>=<span>array();

                </span><span>for</span>($i=<span>0</span>; $i<count($name); $i++<span>){
                    </span><span>if</span>($<span>this</span>-><span>setFiles($name[$i], $tmp_name[$i], $size[$i], $error[$i])){
                        </span><span>if</span>(!$<span>this</span>->checkFileSize() || !$<span>this</span>-><span>checkFileType()){
                            $errors[]</span>=$<span>this</span>-><span>getError();
                            $</span><span>return</span>=<span>false</span><span>;
                        }
                    }</span><span>else</span><span>{
                        $error[]</span>=$<span>this</span>-><span>getError();
                        $</span><span>return</span>=<span>false</span><span>;
                    }

                    </span><span>if</span>(!$<span>return</span><span>)
                        $</span><span>this</span>-><span>setFiles();
                }

                </span><span>if</span>($<span>return</span><span>){
                    $fileNames</span>=<span>array();

                    </span><span>for</span>($i=<span>0</span>; $i<count($name); $i++<span>){
                        </span><span>if</span>($<span>this</span>-><span>setFiles($name[$i], $tmp_name[$i], $size[$i], $error[$i])){
                            $</span><span>this</span>-><span>setNewFileName();
                            </span><span>if</span>(!$<span>this</span>-><span>copyFile()){
                                $errors</span>=$<span>this</span>-><span>getError();
                                $</span><span>return</span>=<span>false</span><span>;
                            }</span><span>else</span><span>{
                                $fileNames[]</span>=$<span>this</span>-><span>newFileName;
                            }
                        }
                    }

                    $</span><span>this</span>->newFileName=<span>$fileNames;
                }

                $</span><span>this</span>->errorMess=<span>$errors;
                </span><span>return</span> $<span>return</span><span>;
            } </span><span>else</span><span> {
                
                    </span><span>if</span>($<span>this</span>-><span>setFiles($name, $tmp_name, $size, $error)){
                        </span><span>if</span>($<span>this</span>->checkFileSize() && $<span>this</span>-><span>checkFileType()){
                            $</span><span>this</span>-><span>setNewFileName();

                            </span><span>if</span>($<span>this</span>-><span>copyFile()){
                                </span><span>return</span> <span>true</span><span>;
                            }</span><span>else</span><span>{
                                $</span><span>return</span>=<span>false</span><span>;
                            }
                                
                        }</span><span>else</span><span>{
                            $</span><span>return</span>=<span>false</span><span>;
                        }    
                    }</span><span>else</span><span>{
                        $</span><span>return</span>=<span>false</span><span>;
                    }
                    
                    

                    </span><span>if</span>(!$<span>return</span><span>)
                        $</span><span>this</span>->errorMess=$<span>this</span>-><span>getError();


                    </span><span>return</span> $<span>return</span><span>;
            }            
        }

        </span><span>private</span><span> function copyFile(){
            </span><span>if</span>(!$<span>this</span>-><span>errorNum){
                $filepath</span>=rtrim($<span>this</span>->filepath, <span>'</span><span>/</span><span>'</span>).<span>'</span><span>/</span><span>'</span><span>;
                $filepath.</span>=$<span>this</span>-><span>newFileName;

                </span><span>if</span>(@move_uploaded_file($<span>this</span>-><span>tmpFileName, $filepath))    {
                    </span><span>return</span> <span>true</span><span>;
                }</span><span>else</span><span>{
                    $</span><span>this</span>->setOption(<span>'</span><span>errorNum</span><span>'</span>, -<span>3</span><span>);
                    </span><span>return</span> <span>false</span><span>;
                }
                    
            }</span><span>else</span><span>{
                </span><span>return</span> <span>false</span><span>;
            }
        }

        </span><span>//</span><span>设置和$_FILES有关的内容</span>
        <span>private</span> function setFiles($name=<span>""</span>, $tmp_name=<span>''</span>, $size=<span>0</span>, $error=<span>0</span><span>){
        
            $</span><span>this</span>->setOption(<span>'</span><span>errorNum</span><span>'</span><span>, $error);
                
            </span><span>if</span><span>($error){
                </span><span>return</span> <span>false</span><span>;
            }

            $</span><span>this</span>->setOption(<span>'</span><span>originName</span><span>'</span><span>, $name);
            $</span><span>this</span>->setOption(<span>'</span><span>tmpFileName</span><span>'</span><span>, $tmp_name);
            $arrStr</span>=explode(<span>'</span><span>.</span><span>'</span><span>, $name); 
            $</span><span>this</span>->setOption(<span>'</span><span>fileType</span><span>'</span>, strtolower($arrStr[count($arrStr)-<span>1</span><span>]));
            $</span><span>this</span>->setOption(<span>'</span><span>fileSize</span><span>'</span><span>, $size);    

            </span><span>return</span> <span>true</span><span>;
        }    

        </span><span>//</span><span>用于获取上传后文件的文件名</span>
<span>        function getNewFileName(){
            </span><span>return</span> $<span>this</span>-><span>newFileName;
        }
        </span><span>//</span><span>上传如果失败,则调用这个方法,就可以查看错误报告</span>
<span>        function getErrorMsg() {
            </span><span>return</span> $<span>this</span>-><span>errorMess;
        }
    }</span>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1008857.htmlTechArticlePHP design super easy-to-use file upload processing class one (37), ? php class FileUpload { private $filepath; //Specify the path to save the uploaded file private $allowtype=array( ' gif ' , '...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

See all articles