up.php
[php]<?php
$files =array ('file1', 'file2','file3','file4','file5');//文件个数
require ("UploadFiles.class.php");
if ( isset ( $_POST['ok'] ) ) {
$Upload = new Upload();
$Upload -> Files = $files;
$Upload -> Dir = './uploads';
$Upload -> UnicName = 1; //是否重命名1 为是,0为否
$Upload -> AutoChmod = 1;
$Upload -> Chmod =0666;
$Upload -> CheckFile = 2;
$Upload -> FilesExt = array ( 'gif', 'jpg' ,'png' ,'zip');//支持的格式
$Upload -> MaxFileSize = 1024*1024;
$Upload -> DoUpload();
print_r ($Upload -> UploadedFiles);
}
$head =<<
FOT;
echo $head.$body.$foot;
?>
back[/php]
UploadFiles.class.php
[php]<?php
/*
+---------------------------------------------
| UploadFiles.class.php
| ==========================================
| by tRiNEX
| 2005 (c)
| ==========================================
| Web: http://www.trinex.ru
| Email: trinex@yandex.ru
+---------------------------------------------
|
| > Upload Files with web-interface.
| > Date started: 18 Jule, 2005
| > Filename: UploadFiles.class.php
| > Version: 1.1
|
+---------------------------------------------
*/
Class Upload {
/**
* @var array of string
*/
var $Files = array ( 'filename' );
/**
* @var string
*/
var $Dir = './uploads';
/**
* @var integer
*/
var $UnicName = 1;
/**
* @var integer
*/
var $AutoChmod = 1;
/**
* @var integer
*/
var $Chmod = 0644;
/**
* @var integer
*/
var $CheckFile = 2;
/**
* @var array of string
*/
var $FilesExt = array ( 'gif', 'jpg', 'bmp' );
/**
* @var integer
*/
var $MaxFileSize = 1024000000;
/**
* @var array of string
*/
var $UploadedFiles;
/**
out time
**/
var $time =120;
/**
* @var array of string
*/
var $ErrorsDesc = array (
0 => 'Value: 0; There is no error, the file uploaded with success.',
1 => 'Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.',
2 => 'Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
3 => 'Value: 3; The uploaded file was only partially uploaded.',
4 => 'Value: 4; No file was uploaded.',
5 => 'Value: 5; No file to upload',
6 => 'Value: 6; Missing a temporary folder.',
7 => 'Value: 7; Error of file extension.',
8 => 'Value: 8; Error of file size',
9 => 'Value: 9; Error of existing file. File already exist.'
);
/**
*
DoUpload file
*/
function DoUpload () {
@ set_time_limit ( $this->$time );
foreach ( $this -> Files as $value ) {
$flag = false;
$filename = basename ( $_FILES[$value]['name'] );
$file = $this -> _split_filename ( $filename );
$ext = $file['ext'];
$body = $file['body'];
if ( ! in_array ( $ext, $this -> FilesExt ) ) {
$this -> UploadedFiles[$value]['ErrorMsg'] = $this -> ErrorsDesc[7];
$flag = true;
}
if ( $_FILES[$value]['name'] == '' ) {
$this -> UploadedFiles[$value]['ErrorMsg'] = $this -> ErrorsDesc[5];
$flag = true;
}
if ( $_FILES[$value]['size'] > $this -> MaxFileSize ) {
$this -> UploadedFiles[$value]['ErrorMsg'] = $this -> ErrorsDesc[8];
$flag = true;
}
$newname = ( $this -> UnicName == 1) ? date("YmdGis").'_'.mt_rand(100, 999).'.'.$ext : $filename;
$file = $this -> _split_filename ( $newname );
$body = $file['body'];
$ext = $file['ext'];
if ( is_file ( $this->Dir."/".$newname ) ) {
if ( $this -> CheckFile == 1 ) {
$flag = true;
$this -> UploadedFiles[$value]['ErrorMsg'] = $this -> ErrorsDesc[9];
} elseif ( $this -> CheckFile == 2 ) {
$i = 0;
while ( is_file ( $this->Dir."/".$newname ) ) {
$i++;
$newname = md5(time()).$body.'_'.$i.'.'.$ext;
}
}
}
if ( ! $flag ) {
if ( move_uploaded_file( $_FILES[$value]['tmp_name'], $this -> Dir."/".$newname ) ) {
echo $this->changename;
$this -> UploadedFiles[$value]['Status'] = 1;
$this -> UploadedFiles[$value]['NewName'] = $newname;
$this -> UploadedFiles[$value]['Size'] = $_FILES[$value]['size'];
($this -> AutoChmod == 1 ) ? @chmod ($this -> Dir."/".$newname, $this -> Chmod ) : '' ;
} else {
$this -> UploadedFiles[$value]['ErrorMsg'] = $this -> ErrorsDesc[$_FILES[$value]['error']];
}
}
}
/**
* image.gif -> ext = gif; body = image;
*
*/}
function _split_filename ( $file ) {
$file = explode ( '.', $file);
$ext = $file[ ( count ( $file ) - 1 ) ];
unset ($file[ ( count ( $file ) - 1 ) ] );
$name = implode ( '.', $file);
$ret['ext'] = $ext;
$ret['body'] = $name;
return $ret;
}
}
?> [/php]