Location: PHPKode > scripts > Multi Upload > multi-upload/upload.class.php
<?
///////////////////////////////////////
//multi-files upload class
//author:paul.ren 
//e-mail:hide@address.com
//website:www.yawill.com
//create:2004-6-22 09:56
//modify:
////////////////////////////////////////

class ClsUpload{
	var $max_upload_size = "1048576";	//max upload file byte (1M)
	var $_FILES = "";			//post files
	var $overwrite = "1";		//is over write
	var $mode = 0777;			//upload file mode
	var $results = array();		//uploaded files results
	
	var $error  = "";
	
	function ClsUpload(){
		global $_FILES;
		$this->_FILES = $_FILES;
	}
	function uploadfile($srcfile,$dstfile){
		if(is_file($dstfile)){
			if($this->overwrite){
				@unlink($dstfile) or $this->error .= "unable to overwrite $dstfile<br>";
			}else return 1;
		}
		@copy($srcfile,$dstfile) or $this->error .= "unable to upload $srcfile to $dstfile<br>";
		$isok = @chmod($dstfile,$this->mode) or $this->error .= is_file($dstfile)."change permissions for:$dstfile<br>";
		return $isok;
	}
	
	function save($field,$dir,$file_name=""){
		$size = $this->_FILES[$field]['size'];
		if(is_array($size))		$size=0;
		if($size>0 && $size<=$this->max_upload_size){
			$this->error = $this->_FILES[$field]['error'];
			$tmp_file  = $this->_FILES[$field]['tmp_name'];
			$file_type = $this->_FILES[$field]['type'];
			if(!$file_name) $file_name = $this->_FILES[$field]['name'];
			$save_path_file = $dir."/".strtolower($file_name);
			$isok = $this->uploadfile($tmp_file,$save_path_file);
			if($isok) $this->results[] = array($this->_FILES[$field]['name']=>$save_path_file);
			return $isok;
		}else if($size>$this->max_upload_size){
			$this->error .= "file size($size) exceeds max file size: $this->max_upload_size<br>";
		}else if($size==0) $this->error .= "File size is 0 bytes<br>";
		return 0;
	}
	function save_as($field,$pathfile){
		$dir = dirname($pathfile);
		$filename = basename($pathfile);
		return $this->save($field,$dir,$filename);
	}
	function mult_save($field,$dir){
		$field_names = $this->_FILES[$field]['name'];
		if(is_array($field_names)){
			while(list($key,$file_name)=each($field_names)){
				if(!$file_name) continue;
				$tmp_file = $this->_FILES[$field]['tmp_name'][$key];
				$file_name = strtolower($file_name);
				$file_size = $this->_FILES[$field]['size'][$key];
				if($file_size>0 && $file_size<=$this->max_upload_size){
					$isok = $this->uploadfile($tmp_file,"$dir/$file_name");
				}else if($file_size>$this->max_upload_size){
					$this->error .= "$file_name:file size exceeds max file size: $this->max_upload_size<br>";
				}else $this->error .= "$file_name:File size is 0 bytes<br>";
				if($isok) $this->results[] = array($file_name=>"$dir/$file_name");
			}
		}else {
			return $this->save($field,$dir);
		}
		return $isok;
	}
	function mult_save_as($field,$dir,$prefix="up_"){
		$field_names = $this->_FILES[$field]['name'];
		if(is_array($field_names)){
			while(list($key,$fname)=each($field_names)){
				if(!$fname) continue;
				$tmp_file = $this->_FILES[$field]['tmp_name'][$key];
				$file_name = strtolower("$prefix".$key.strrchr($fname,"."));
				$file_size = $this->_FILES[$field]['size'][$key];
				if($file_size>0 && $file_size <= $this->max_upload_size){
					$isok = $this->uploadfile($tmp_file,"$dir/$file_name");
				}else if($file_size>$this->max_upload_size){
					$this->error .= "$file_name:file size exceeds max file size: $this->max_upload_size<br>";
				}else $this->error .= "$file_name:File size is 0 bytes<br>";
				if($isok) $this->results[] = array($fname=>"$dir/$file_name");
			}
		}else {
			$filename = $prefix? ($prefix.strrchr(basename($this->_FILES[$field]['name']))):"";
			return $this->save($field,$dir,$filename);
		}
		return $isok;
	}
	function p($data){
		echo "<pre>";
		print_r($data);
		echo "</pre>";
	}
}

?>
Return current item: Multi Upload