先來看看,例子調用方法:
:
下面來看看代碼.先上傳類?/*
TITLE : CLASS Upload
FILE : class.upload.php
DESCRIPTION : To provide upload utility,
AUTHOR : Peng Zhang zpadmin()gmail.com http://blog.neten.de
BASED ON : whxbb whxbb()21cn.com
WRITED : 2005 NOV 20
MODIFIED : 2006 MAR 01
modify by psdshow (psdshow(at)yahoo.com.cn) 2007-11-30
LICENCE : GPL
REVISION : V1.0.2
*/
class Upload{
var $saveName;// 儲存名
var $savePath;// 儲存路徑
var $fileFormat = array('gif','jpg','doc','application/octet-stream');// 檔案格式&MIME限定
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();// 每個檔案返回資訊
// 建構函式
// @param $savePath 檔案儲存路徑
// @param $fileFormat 檔案格式限制數組
// @param $maxSize 檔案最大尺寸
// @param $overwriet 是否覆蓋 1 允許覆蓋 0 禁止覆蓋
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;
}
// 上傳
// @param $fileInput 網頁Form(表單)中input的名稱
// @param $changeName 是否變更檔名
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->ext,下次迴圈會更新
$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;
}
}
// 單個檔案上傳
// @param $fileArray 檔案資訊數組
function copyfile($fileArray){
$this->returninfo = array();
// 返回資訊
$this->returninfo['name'] = $fileArray['name'];
$this->returninfo['saveName'] = $this->saveName;
$this->returninfo['size'] = number_format( ($fileArray['size'])/1024 , 0, '.', ' ');//以KB為單位
$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->overwrite == 0 && @file_exists($this->savePath.$fileArray['name'])){
// $this->errno = 13;
// 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;
/*
if (($originalHeight < $this->thumbHeight
&& $originalWidth < $this->thumbWidth)) {
// 如果比期望的縮圖小,那隻Copy
move_uploaded_file($this->savePath.$this->saveName,
$this->savePath.$this->thumbPrefix.$this->saveName);
} else {
if( $originalWidth > $this->thumbWidth ){// 寬 > 設定寬度
$thumbWidth = $this->thumbWidth ;
$thumbHeight = $this->thumbWidth * ( $originalHeight / $originalWidth );
if($thumbHeight > $this->thumbHeight){// 高 > 設定高度
$thumbWidth = $this->thumbHeight * ( $thumbWidth / $thumbHeight );
$thumbHeight = $this->thumbHeight ;
}
}elseif( $originalHeight > $this->thumbHeight ){// 高 > 設定高度
$thumbHeight = $this->thumbHeight ;
$thumbWidth = $this->thumbHeight * ( $originalWidth / $originalHeight );
if($thumbWidth > $this->thumbWidth){// 寬 > 設定寬度
$thumbHeight = $this->thumbWidth * ( $thumbHeight / $thumbWidth );
$thumbWidth = $this->thumbWidth ;
}
}
*/
$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;}
}
// 刪除臨時檔案
/*
if(!@$this->del($fileArray["tmp_name"])){
return false;
}
*/
return true;
}
// 檔案格式檢查,MIME檢測
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;
}
// 擷取副檔名
// @param $fileName 上傳檔案的原檔案名稱
function getExt($fileName){
$ext = explode(".", $fileName);
$ext = $ext[count($ext) - 1];
$this->ext = strtolower($ext);
}
// 設定上傳檔案的最大位元組限制
// @param $maxSize 檔案大小(bytes) 0:表示無限制
function setMaxsize($maxSize){
$this->maxSize = $maxSize;
}
// 設定檔案格式限定
// @param $fileFormat 檔案格式數組
function setFileformat($fileFormat){
if(is_array($fileFormat)){$this->fileFormat = $fileFormat ;}
}
// 設定覆蓋模式
// @param overwrite 覆蓋模式 1:允許覆蓋 0:禁止覆蓋
function setOverwrite($overwrite){
$this->overwrite = $overwrite;
}
// 設定儲存路徑
// @param $savePath 檔案儲存路徑:以 "/" 結尾,若沒有 "/",則補上
function setSavepath($savePath){
$this->savePath = substr( str_replace("\","/", $savePath) , -1) == "/"
? $savePath : $savePath."/";
}
// 設定縮圖
// @param $thumb = 1 產生縮圖 $thumbWidth,$thumbHeight 是縮圖的寬和高
function setThumb($thumb, $thumbWidth = 0,$thumbHeight = 0){
$this->thumb = $thumb;
if($thumbWidth) $this->thumbWidth = $thumbWidth;
if($thumbHeight) $this->thumbHeight = $thumbHeight;
}
// 設定檔案儲存名
// @param $saveName 儲存名,如果為空白,則系統自動產生一個隨機的檔案名稱
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;
}
// 刪除檔案
// @param $fileName 所要刪除的檔案名稱
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];
}
}
?>
下面看看是怎麼調用 的.:
//如果收到表單傳來的參數,則進行上傳處理,否則顯示表單
if(isset($_FILES['uploadinput'])){
//建目錄函式,其中參數$directoryName最後沒有"/",
//要是有的話,以'/'打散為數組的時候,最後將會出現一個空值
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 ('class.upload.php');
//設定檔案上傳目錄
$savePath = "upload";
//建立目錄
makeDirectory($savePath);
//允許的檔案類型
$fileFormat = array('gif','jpg','jpge','png');
//檔案大小限制,單位: Byte,1KB = 1000 Byte
//0 表示無限制,但受php.ini中upload_max_filesize設定影響
$maxSize = 0;
//覆蓋原有檔案嗎? 0 不允許 1 允許
$overwrite = 0;
//初始化上傳類
$f = new Upload( $savePath, $fileFormat, $maxSize, $overwrite);
//如果想產生縮圖,則調用成員函數 $f->setThumb();
//參數列表: setThumb($thumb, $thumbWidth = 0,$thumbHeight = 0)
//$thumb=1 表示要產生縮圖,不調用時,其值為 0
//$thumbWidth 縮圖寬,單位是像素(px),留空則使用預設值 130
//$thumbHeight 縮圖高,單位是像素(px),留空則使用預設值 130
$f->setThumb(1);
//參數中的uploadinput是表單中上傳檔案輸入框input的名字
//後面的0表示不變更檔名,若為1,則由系統產生隨機檔案名稱
if (!$f->run('uploadinput',1)){
//通過$f->errmsg()只能得到最後一個出錯的資訊,
//詳細的資訊在$f->getInfo()中可以得到。
echo $f->errmsg()."
n";
}
//上傳結果儲存在數組returnArray中。
echo "
";
print_r($f->getInfo());
echo "
";
}
}else{
?>
}
//我們上傳一個已經存在了的圖片檔案,
//一個正常的圖片檔案,和一個不允許上傳的檔案,
//輸出結果如下
/*
The uploaded file is Unallowable!
Array
(
[0] => Array
(
[name] => boy.jpg
[saveName] => boy.jpg
[size] => 137
[type] => image/pjpeg
[error] => File exist already!
)
[1] => Array
(
[name] => girl.JPG
[saveName] => girl.JPG
[size] => 31
[type] => image/pjpeg
[originalHeight] => 450
[originalWidth] => 600
)
[2] => Array
(
[name] => test.wma
[saveName] => test.wma
[size] => 971
[type] => audio/x-ms-wma
[error] => The uploaded file is Unallowable!
)
)
*/
?>
http://www.bkjia.com/PHPjc/445081.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445081.htmlTechArticle先來看看,例子調用方法: : 下面來看看代碼.先上傳類??php /* TITLE : CLASS Upload FILE : class.upload.php DESCRIPTION : To provide upload utility, AUTHOR...