Maison > développement back-end > tutoriel php > PHP设计超级好用的文件上传处理类一 (37),_PHP教程

PHP设计超级好用的文件上传处理类一 (37),_PHP教程

WBOY
Libérer: 2016-07-13 09:52:21
original
905 Les gens l'ont consulté

PHP设计超级好用的文件上传处理类一 (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>
Copier après la connexion

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1008857.htmlTechArticlePHP设计超级好用的文件上传处理类一 (37), ? php class FileUpload { private $filepath; // 指定上传文件保存的路径 private $allowtype=array( ' gif ' , '...
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal