实现多文件上传php类 |
时间:2015-01-23 来源:西部数据 作者:西部数据 |
|
多文件上传是PHP中的一个基础应用,反正PHPer都会遇到的问题,现在就介绍一个功能完善、强大的多文件上传类给大家吧,能用上这个类的地方会很多,代码如下:
- <?php
- class Upload{
- var $saveName;
- var $savePath;
- var $fileFormat = array('gif','jpg','doc','application/octet-stream');
- var $overwrite = 0;
- var $maxSize = 0;
- var $ext;
- var $thumb = 0;
- var $thumbWidth = 130;
- var $thumbHeight = 130;
- var $thumbPrefix = "_thumb_";
- var $errno;
- var $returnArray= array();
- var $returninfo= array();
-
-
-
-
-
-
- function Upload($savePath, $fileFormat='',$maxSize = 0, $overwrite = 0) {
- $this->setSavepath($savePath);
- $this->setFileformat($fileFormat);
- $this->setMaxsize($maxSize);
- $this->setOverwrite($overwrite);
- $this->setThumb($this->thumb, $this->thumbWidth,$this->thumbHeight);
- $this->errno = 0;
- }
-
-
-
- function run($fileInput,$changeName = 1){
- if(isset($_FILES[$fileInput])){
- $fileArr = $_FILES[$fileInput];
- if(is_array($fileArr['name'])){
- for($i = 0; $i < count($fileArr['name']); $i++){
- $ar['tmp_name'] = $fileArr['tmp_name'][$i];
- $ar['name'] = $fileArr['name'][$i];
- $ar['type'] = $fileArr['type'][$i];
- $ar['size'] = $fileArr['size'][$i];
- $ar['error'] = $fileArr['error'][$i];
- $this->getExt($ar['name']);
- $this->setSavename($changeName == 1 ? '' : $ar['name']);
- if($this->copyfile($ar)){
- $this->returnArray[] = $this->returninfo;
- }else{
- $this->returninfo['error'] = $this->errmsg();
- $this->returnArray[] = $this->returninfo;
- }
- }
- return $this->errno ? false : true;
- }else{
- $this->getExt($fileArr['name']);
- $this->setSavename($changeName == 1 ? '' : $fileArr['name']);
- if($this->copyfile($fileArr)){
- $this->returnArray[] = $this->returninfo;
- }else{
- $this->returninfo['error'] = $this->errmsg();
- $this->returnArray[] = $this->returninfo;
- }
- return $this->errno ? false : true;
- }
- return false;
- }else{
- $this->errno = 10;
- return false;
- }
- }
-
-
- function copyfile($fileArray){
- $this->returninfo = array();
-
- $this->returninfo['name'] = $fileArray['name'];
- $this->returninfo['md5'] = @md5_file($fileArray['tmp_name']);
- $this->returninfo['saveName'] = $this->saveName;
- $this->returninfo['size'] = number_format( ($fileArray['size'])/1024 , 0, '.', ' ');
- $this->returninfo['type'] = $fileArray['type'];
-
- if (!$this->validateFormat()){
- $this->errno = 11;
- return false;
- }
-
- if(!@is_writable($this->savePath)){
- $this->errno = 12;
- return false;
- }
-
-
-
-
-
-
- if ($this->maxSize != 0 ){
- if ($fileArray["size"] > $this->maxSize){
- $this->errno = 14;
- return false;
- }
- }
-
- if(!@move_uploaded_file($fileArray["tmp_name"], $this->savePath.$this->saveName)){
- $this->errno = $fileArray["error"];
- return false;
- }elseif( $this->thumb ){
- $CreateFunction = "imagecreatefrom".($this->ext == 'jpg' ? 'jpeg' : $this->ext);
- $SaveFunction = "image".($this->ext == 'jpg' ? 'jpeg' : $this->ext);
- if (strtolower($CreateFunction) == "imagecreatefromgif"
- && !function_exists("imagecreatefromgif")) {
- $this->errno = 16;
- return false;
- } elseif (strtolower($CreateFunction) == "imagecreatefromjpeg"
- && !function_exists("imagecreatefromjpeg")) {
- $this->errno = 17;
- return false;
- } elseif (!function_exists($CreateFunction)) {
- $this->errno = 18;
- return false;
- }
-
- $Original = @$CreateFunction($this->savePath.$this->saveName);
- if (!$Original) {$this->errno = 19; return false;}
- $originalHeight = ImageSY($Original);
- $originalWidth = ImageSX($Original);
- $this->returninfo['originalHeight'] = $originalHeight;
- $this->returninfo['originalWidth'] = $originalWidth;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $radio=max(($originalWidth/$this->thumbWidth),($originalHeight/$this->thumbHeight));
- $thumbWidth=(int)$originalWidth/$radio;
- $thumbHeight=(int)$originalHeight/$radio;
- if ($thumbWidth == 0) $thumbWidth = 1;
- if ($thumbHeight == 0) $thumbHeight = 1;
- $createdThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
- if ( !$createdThumb ) {$this->errno = 20; return false;}
- if ( !imagecopyresampled($createdThumb, $Original, 0, 0, 0, 0,
- $thumbWidth, $thumbHeight, $originalWidth, $originalHeight) )
- {$this->errno = 21; return false;}
- if ( !$SaveFunction($createdThumb,
- $this->savePath.$this->thumbPrefix.$this->saveName) )
- {$this->errno = 22; return false;}
-
- }
-
-
-
-
-
-
- return true;
- }
-
- function validateFormat(){
- if(!is_array($this->fileFormat)
- || in_array(strtolower($this->ext), $this->fileFormat)
- || in_array(strtolower($this->returninfo['type']), $this->fileFormat) )
- return true;
- else
- return false;
- }
-
-
- function getExt($fileName){
- $ext = explode(".", $fileName);
- $ext = $ext[count($ext) - 1];
- $this->ext = strtolower($ext);
- }
-
-
- function setMaxsize($maxSize){
- $this->maxSize = $maxSize;
- }
-
-
- function setFileformat($fileFormat){
- if(is_array($fileFormat)){$this->fileFormat = $fileFormat ;}
- }
-
-
- function setOverwrite($overwrite){
- $this->overwrite = $overwrite;
- }
-
-
-
- function setSavepath($savePath){
- $this->savePath = substr( str_replace("\","/", $savePath) , -1) == "/"
- ? $savePath : $savePath."/";
- }
-
-
- function setThumb($thumb, $thumbWidth = 0,$thumbHeight = 0){
- $this->thumb = $thumb;
- if($thumbWidth) $this->thumbWidth = $thumbWidth;
- if($thumbHeight) $this->thumbHeight = $thumbHeight;
- }
-
-
- function setSavename($saveName){
- if ($saveName == ''){
- $name = date('YmdHis')."_".rand(100,999).'.'.$this->ext;
-
- if(file_exists($this->savePath . $name)){
- $name = setSavename($saveName);
- }
- } else {
- $name = $saveName;
- }
- $this->saveName = $name;
- }
-
-
- function del($fileName){
- if(!@unlink($fileName)){
- $this->errno = 15;
- return false;
- }
- return true;
- }
-
- function getInfo(){
- return $this->returnArray;
- }
-
- function errmsg(){
- $uploadClassError = array(
- 0 =>'There is no error, the file uploaded with success. ',
- 1 =>'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
- 2 =>'The uploaded file exceeds the MAX_FILE_SIZE that was specified in the HTML form.',
- 3 =>'The uploaded file was only partially uploaded. ',
- 4 =>'No file was uploaded. ',
- 6 =>'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3. ',
- 7 =>'Failed to write file to disk. Introduced in PHP 5.1.0. ',
- 10 =>'Input name is not unavailable!',
- 11 =>'The uploaded file is Unallowable!',
- 12 =>'Directory unwritable!',
- 13 =>'File exist already!',
- 14 =>'File is too big!',
- 15 =>'Delete file unsuccessfully!',
- 16 =>'Your version of PHP does not appear to have GIF thumbnailing support.',
- 17 =>'Your version of PHP does not appear to have JPEG thumbnailing support.',
- 18 =>'Your version of PHP does not appear to have pictures thumbnailing support.',
- 19 =>'An error occurred while attempting to copy the source image .
- Your version of php ('.phpversion().') may not have this image type support.',
- 20 =>'An error occurred while attempting to create a new image.',
- 21 =>'An error occurred while copying the source image to the thumbnail image.',
- 22 =>'An error occurred while saving the thumbnail image to the filesystem.
- Are you sure that PHP has been configured with both read and write access on this folder?',
- );
- if ($this->errno == 0)
- return false;
- else
- return $uploadClassError[$this->errno];
- }
- }
- ?>
- 如何使用这个类呢?
- <?php
-
- if(isset($_FILES['uploadinput'])){
-
-
- function makeDirectory($directoryName) {
- $directoryName = str_replace("\","/",$directoryName);
- $dirNames = explode('/', $directoryName);
- $total = count($dirNames) ;
- $temp = '';
- for($i=0; $i<$total; $i++) {
- $temp .= $dirNames[$i].'/';
- if (!is_dir($temp)) {
- $oldmask = umask(0);
- if (!mkdir($temp, 0777)) exit("不能建立目录 $temp");
- umask($oldmask);
- }
- }
- return true;
- }
- if($_FILES['uploadinput']['name'] <> ""){
-
- require_once ('upload_class.php');
-
- $savePath = "upload";
-
- makeDirectory($savePath);
-
- $fileFormat = array('gif','jpg','jpge','png');
-
-
- $maxSize = 0;
-
- $overwrite = 0;
-
- $f = new Upload( $savePath, $fileFormat, $maxSize, $overwrite);
-
-
-
-
-
- $f->setThumb(1);
-
-
-
- if (!$f->run('uploadinput',1)){
-
-
- echo $f->errmsg()."<br>n";
- }
-
- echo "<pre>";
- print_r($f->getInfo());
- echo "</pre>";
- }
- }else{
- ?>
- <form enctype="multipart/form-data" action="" method="POST">
- Send this file: <br />
- <input name="uploadinput[]" type="file"><br />
- <input name="uploadinput[]" type="file"><br />
- <input name="uploadinput[]" type="file"><br />
- <input type="submit" value="Send File"><br />
- </form>
- <?php
- }
- ?>
|
|
|
|