Maison > Article > développement back-end > PHP上传类实现单个和批量上传_PHP教程
PHP上传类还是比较常用的,于是我研究了一下PHP上传类,在这里拿出来和大家分享一下,希望对大家有用。PHP本身是一种简单而强大的语言。PHP语言拥有核心特性如强大的字符串和数组处理能力,同时极大的改进了对面向对象编程的支持(PHP5以上版本)。
PHP上传类代码:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span> </span></span></li> <li class=""><span>/** </span></li> <li class="alt"><span>*@packagemyFrameworkuploadclass </span></li> <li class=""><span>*@Descriptionuploadclass </span></li> <li class="alt"><span>*@Date2007-11-28 </span></li> <li class=""><span>*@authorantsnet </span></li> <li class="alt"><span>*@copyrighthttp://www.antsnet.net </span></li> <li class=""><span>*@Emailantsnet@163.com </span></li> <li class="alt"><span>*@Environment:Apache2.0.59+PHP5.2.5+mysql5.0 </span></li> <li class=""><span>*@version$Id:myFrame_Upload.php22008-02-2701:14:05ZAdministrator$ </span></li> <li class="alt"><span>*/ </span></li> <li class=""><span>classmyFrame_UploadextendsmyFrame </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>var$</span><span class="attribute">uploadPath</span><span>=</span><span class="attribute-value">"uploadFile/"</span><span>; </span> </li> <li class="alt"> <span>var$</span><span class="attribute">fullPath</span><span>=</span><span class="attribute-value">''</span><span>; </span> </li> <li class=""><span>var$message; </span></li> <li class="alt"> <span>var$</span><span class="attribute">_debug</span><span>=</span><span class="attribute-value">false</span><span>; </span> </li> <li class=""> <span>var$</span><span class="attribute">errorMessage</span><span>=</span><span class="attribute-value">''</span><span>; </span> </li> <li class="alt"><span> </span></li> <li class=""> <span>function__construct($</span><span class="attribute">uploadPath</span><span>=</span><span class="attribute-value">''</span><span>) </span> </li> <li class="alt"><span>{ </span></li> <li class=""><span>if($uploadPath!="") </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>$this-</span><span class="tag">></span><span class="attribute">uploadPath</span><span>=$uploadPath; </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span>/** </span></li> <li class=""><span>*Batchupload </span></li> <li class="alt"><span>* </span></li> <li class=""><span>*@paramArray$arrayOutPut </span></li> <li class="alt"><span>*/ </span></li> <li class=""> <span>publicfunctionformLocalBatch($</span><span class="attribute">keepSource</span><span>=</span><span class="attribute-value">false</span><span>,$</span><span class="attribute">arrayOutPut</span><span>=</span><span class="attribute-value">''</span><span>) </span> </li> <li class="alt"><span>{ </span></li> <li class=""> <span>$</span><span class="attribute">returnArray</span><span>=</span><span class="attribute-value">array</span><span>(); </span> </li> <li class="alt"><span>if(sizeof($_FILES)==$arrayOutPut&&!$keepSource) </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>$</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>; </span> </li> <li class=""> <span>foreach($_FILESas$</span><span class="attribute">index</span><span>=</span><span class="tag">></span><span>$value) </span> </li> <li class="alt"><span>{ </span></li> <li class=""> <span>$returnArray[]=$this-</span><span class="tag">></span><span>fromLocal($value,$outPutName[$i]); </span> </li> <li class="alt"><span>$i++; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>}else{ </span></li> <li class=""> <span>foreach($_FILESas$</span><span class="attribute">index</span><span>=</span><span class="tag">></span><span>$value) </span> </li> <li class="alt"><span>{ </span></li> <li class=""> <span>$returnArray[]=$this-</span><span class="tag">></span><span>fromLocal($value); </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span>return$returnArray; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>/** </span></li> <li class=""><span>*Uploadfileformlocal </span></li> <li class="alt"><span>* </span></li> <li class=""><span>*@paramArray|String$file_Area_Name </span></li> <li class="alt"><span>*@paramArray|String$outPutName </span></li> <li class=""><span>*/ </span></li> <li class="alt"> <span>publicfunctionfromLocal($VALUE,$</span><span class="attribute">outPutName</span><span>=</span><span class="attribute-value">''</span><span>) </span> </li> <li class=""><span>{ </span></li> <li class="alt"><span> </span></li> <li class=""><span>include_once(SERVERROOT.MYFRAME.'myFrame_Basic.php'); </span></li> <li class="alt"><span>/** </span></li> <li class=""><span>*thefollowingforsingle </span></li> <li class="alt"><span>*/ </span></li> <li class=""> <span>if($</span><span class="attribute">outPutName</span><span>==''||$</span><span class="attribute">outPutName</span><span>=="NULL") </span> </li> <li class="alt"><span>{ </span></li> <li class=""> <span>$</span><span class="attribute">outPutName</span><span>=</span><span class="attribute-value">date</span><span>("YmdHis"); </span> </li> <li class="alt"><span>} </span></li> <li class=""> <span>if($VALUE['error']</span><span class="tag">></span><span>0) </span> </li> <li class="alt"><span>{ </span></li> <li class=""><span>switch($VALUE['errror']) </span></li> <li class="alt"><span>{ </span></li> <li class=""><span>case'1': </span></li> <li class="alt"> <span>$this-</span><span class="tag">></span><span>errorMessage[]=$this-</span><span class="tag">></span><span>myFrameMessage['false']['file']['max']; </span> </li> <li class=""><span>returnfalse; </span></li> <li class="alt"><span>break; </span></li> <li class=""><span>case'2': </span></li> <li class="alt"> <span>$this-</span><span class="tag">></span><span>errorMessage[]=$this-</span><span class="tag">></span><span>myFrameMessage['false']['file']['maxDefined']; </span> </li> <li class=""><span>returnfalse; </span></li> <li class="alt"><span>break; </span></li> <li class=""><span>case'3': </span></li> <li class="alt"> <span>$this-</span><span class="tag">></span><span>errorMessage[]=$this-</span><span class="tag">></span><span>myFrameMessage['false']['file']['uncomplite']; </span> </li> <li class=""><span>returnfalse; </span></li> <li class="alt"><span>break; </span></li> <li class=""><span>case'4': </span></li> <li class="alt"> <span>$this-</span><span class="tag">></span><span>errorMessage[]=$this-</span><span class="tag">></span><span>myFrameMessage['false']['file']['unupload']; </span> </li> <li class=""><span>returnfalse; </span></li> <li class="alt"><span>break; </span></li> <li class=""><span> </span></li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"> <span>$</span><span class="attribute">fileName</span><span>=$this-</span><span class="tag">></span><span>uploadPath.$outPutName.myFrame_Basic::getFileName($VALUE['name']).myFrame_Basic::getFileExt($VALUE['name']); </span> </li> <li class=""><span>if(is_uploaded_file($VALUE['tmp_name'])) </span></li> <li class="alt"><span>{ </span></li> <li class=""><span>if(!move_uploaded_file($VALUE['tmp_name'],$fileName)) </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>$this-</span><span class="tag">></span><span>errorMessage[]=$this-</span><span class="tag">></span><span>myFrameMessage['false']['file']['move']; </span> </li> <li class="alt"><span>returnfalse; </span></li> <li class=""><span>}else{ </span></li> <li class="alt"><span>return$fileName; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span>/** </span></li> <li class=""><span>*Uploadfromnetwork </span></li> <li class="alt"><span>* </span></li> <li class=""><span>*@paramArray|String$url </span></li> <li class="alt"><span>*@paramArray|String$outPutName </span></li> <li class=""><span>*@paramBool$keepSource </span></li> <li class="alt"><span>*/ </span></li> <li class=""> <span>publicfunctionfromNet($url,$</span><span class="attribute">outPutName</span><span>=</span><span class="attribute-value">''</span><span>,$</span><span class="attribute">keepSource</span><span>=</span><span class="attribute-value">false</span><span>) </span> </li> <li class="alt"><span>{ </span></li> <li class=""><span>include_once(SERVERROOT.MYFRAME.'myFrame_Basic.php'); </span></li> <li class="alt"> <span>if($</span><span class="attribute">outPutName</span><span>=="") </span> </li> <li class=""><span>{ </span></li> <li class="alt"> <span>$</span><span class="attribute">outPutName</span><span>=</span><span class="attribute-value">date</span><span>("YmdHis"); </span> </li> <li class=""><span>} </span></li> <li class="alt"> <span>$</span><span class="attribute">fileType</span><span>=</span><span class="attribute-value">myFrame_Basic</span><span>::getFileExt($url); </span> </li> <li class=""> <span>$</span><span class="attribute">fileName</span><span>=$outPutName.$fileType; </span> </li> <li class="alt"> <span>$</span><span class="attribute">contents</span><span>=</span><span class="attribute-value">file_get_contents</span><span>($url); </span> </li> <li class=""> <span>$</span><span class="attribute">return</span><span>=</span><span class="attribute-value">file_put_contents</span><span>($this-</span><span class="tag">></span><span>uploadPath.$fileName,$contents); </span> </li> <li class="alt"><span>if($return){ </span></li> <li class=""> <span>$this-</span><span class="tag">></span><span class="attribute">fullPath</span><span>=$this-</span><span class="tag">></span><span>uploadPath.$fileName; </span> </li> <li class="alt"> <span>return$this-</span><span class="tag">></span><span>fullPath; </span> </li> <li class=""><span>}else{ </span></li> <li class="alt"> <span>$this-</span><span class="tag">></span><span>errorMessage[]=$this-</span><span class="tag">></span><span>myFrameMessage['false']['file']['url']; </span> </li> <li class=""><span>returnfalse; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span>} </span></li> </ol>