php 檔案上傳類_PHP教程

來源:互聯網
上載者:User
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,'.'));
}
}
?>


http://www.bkjia.com/PHPjc/445069.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445069.htmlTechArticlephp 檔案上傳類 和 php 上傳檔案 以及php 上傳圖片等php 上傳代碼只是表現形式不一樣了。 ?php class Uploader { var $_base_dir = null; var $_rel_dir = nul...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.