php 文件上傳類和php 上傳文件以及php 上傳圖片等php 上傳代碼只是表現形式不一樣了。
<?php
class Uploader
{
var $_base_dir = null;
var $_rel_dir = null;
var $_random_fname = false;
var $_random_fname_len = 5;
var $_fname_filter = null;
var $_ftype_filter = null;
var $_origin_paths = array();
function Uploader( $base_dir, $rel_dir )
{
$this->_base_dir = $base_dir;
$this->_rel_dir = $rel_dir;
}
function setRandomFileName($random_fname, $random_fname_len=5)
{
$this->_random_fname = $random_fname;
$this->_random_fname_len = $random_fname_len;
}
function setFileTypeFilter($filter)
{
$this->_ftype_filter = $filter;
}
function addFile($file, $origin_path='')
{
$file = trim($file);
$origin_path = trim($origin_path);
if( array_key_exists($file, $this->_origin_paths) )
return;
$this->_origin_paths[$file] = $origin_path;
}
function upload()
{
foreach( $this->_origin_paths as $file => $origin_path )
{
$result = $this->_uploadFile($file, $origin_path);
if( $result != 'Success' )
return $result;
}
return 'Success';
}
/*
* @desc 上傳附件
* @return 成功返回Success 失敗返回失敗類型
* @param $file 文件名 $orgin_path 文件路徑
*/
function _uploadFile($file, $origin_path) //上傳附件
{
$ffile = $_FILES[$file]['tmp_name']; //文件被上傳後在服務端儲存的臨時文件名。
$fname = $_FILES[$file]['name']; //客戶端機器文件的原名稱。
$fsize = $_FILES[$file]['size']; //已上傳文件的大小
$ftype = $_FILES[$file]['type']; //文件的 MIME 類型
$new_path = '';
if( !empty($fname) && is_uploaded_file($ffile) )
{
if( !empty($this->_ftype_filter) && !is_null($this->_ftype_filter) )
{
$match = false;
$extensions = explode(',', $this->_ftype_filter);
foreach($extensions as $extension)
{
if( strtolower(strrchr($fname,'.')) == '.'.strtolower(trim($extension)) )
{
$match = true;
break;
}
}
if( !$match )
return 'ErrorFileTypeFilterNotMatch';
}
$fpath = $this->_base_dir . $this->_rel_dir;
if( $this->_random_fname )
$fname = $this->_getUniqueFileName($fname, $this->_random_fname_len);
copy( $ffile, $fpath . $fname ) or die( 'upload failed!' );
$new_path = $this->_rel_dir . $fname;
}
if( !empty($origin_path) && !empty($new_path) && $origin_path!=$new_path )
{
$this->delete($origin_path);
}
if( !empty($new_path) )
$this->_origin_paths[$file] = $new_path;
$Erroe=$_FILES[$file]['error'];
switch($Erroe){
case 1:
return 'ErrExceedUploadMaxFileSize';
break;
case 2:
return 'ErrExceedHtmlMaxFileSize';
break;
case 3:
return 'ErrPartFileTrans';
break;
// case 4:
// return 'ErrNoFileTrans';
// break;
default:
return 'Success';
}
}
/*
* @desc 取得路徑
* @return 路徑
* @param 無
*/
function getFilePath()
{
return $this->_origin_paths;
}
function getFileAbsPath()
{
$paths = array();
foreach( $this->_origin_paths as $path )
{
$paths[] = $this->_base_dir . $path;
}
return $paths;
}
function delete( $fpath )
{
if( !empty($fpath) && is_file($this->_base_dir . $fpath) )
unlink( $this->_base_dir . $fpath ) or die( 'unlink error' );
}
function _getUniqueFileName( $fname, $len )
{
$timestamp = date('YmdHis');
srand((double)microtime()*1000000);
for( $i=0, $randfname=''; $i<$len; $i++ )
{
$num = rand(0, 35);
if( $num < 10 )
$randfname .= chr( ord('0')+$num );
else
$randfname .= chr( ord('a')+$num-10 );
}
return $timestamp.'_'.$randfname.strtolower(strrchr($fname,'.'));
}
}
?>