upload($_FILES['img']);//返回上傳後檔案名稱數組,$_FILES['img']為上傳的文件"/> upload($_FILES['img']);//返回上傳後檔案名稱數組,$_FILES['img']為上傳的文件">
搜尋
首頁後端開發php教程PHP5+UTF8多檔案上傳類

還有些功能沒有加上去,如自動更名,圖片處理等.可依需要自行添加.
USE:
$up = new upfile(ROOT_PATH.'data/'.date("Ym" ,time()),array('gif','jpg','jpeg'),true);
$fileimg = $up->upload($_FILES['img']);//返回上傳後檔案名稱數組,$_FILES['img']為上傳的檔案
可使用$up->log查看上傳時資訊.
//========= ===========================================
// FileName: upfile.class.php
// Summary: 檔案上傳類別
// Author: millken(迷路林肯)
// LastModifed: 2008-6-4
// copyright (c)2008 millken @gmail.com
//========================================= ===========
if(!defined('OK'))exit(__FILE__.'Access Denied');
class upfile {
public $ExtensionFileFormat = array() ;
public $returninfo = array();
private $ImageFileFormat = array('gif','bmp','jpg','jpe','jpeg','png');
private $OtherFileFormat = array('zip','pdf','rar','xls','doc','ppt','csv');
private $savePath;
private $attachment_path = '. /upfiles/';
private $overwrite = false; # 同名時是否覆蓋
private $maxSize = 0; # 檔案最大字節,為0時不限制大小
private $ext;
private $errno = 0;
/* 建構子
* (string)$savePath 檔案儲存路徑,預設為$attachment_path
* (array)$extensionFileFormat 自訂上傳檔案的副檔名,未設定時為$ImageFileFormat || $OtherFileFormat
* (bool)$overwrite 是否覆寫同名檔案
*/
public function __construct($savePath='',$extensionFileFormat = array(),$overwrite = false) {
$this->savePath = empty($savePath)?$this->attachment_pathsavePath.'/';
$this->extensionFileFormat = is_array($extensionFileFormat)?$extensionFileFormat:arraFormat = is_array($extensionFileFormat)?$extensionFileFormat:array(); >$this->overwrite = is_bool($overwrite)?$overwrite:false;
}
/*上傳函數
* (array)$files 待上傳的檔案陣列$_FILES['attach']
* (number)$maxSize 檔案的最大位元組數,預設為0不限制上傳大小
*/
public function upload($files,$maxSize=0) {
$this- >maxSize = is_numeric($maxSize)?$maxSize:0;
if(isset($files) && is_array($files)) {
if(is_array($files['name'])) {
foreach($files as $key => $var) {
foreach($var as $id => $val) {
$attachments[$id][$key] = $val;
}
}
} else {
$attachments[] = $files;
}
}
self::check_file_type($attachments);
if(empty(py(ty()) $this->filelist)) {
$this->log .= "待上傳的檔案清單為空。 n";
return array();
}
if(!self::makeDirectory() || !@is_writable($this->savePath)) {
$this->log . = $this->savePath . "不能建立或其權限為不可寫。 n";
return array();
}
$filearray = array();
foreach($this->filelist as $k=>$f) {
if($ this->maxSize && $f['size']>$this->maxSize) {
$this->log .= $f['name'] . "其大小超過了設定的值:" . $this->maxSize ."n";
}elseif($this->overwrite == false && file_exists($this->savePath . $f['name'])) {
$this-> log .= $f['name'] . "已經存在於目錄:" . $this->savePath . "n";
}else{
@unlink($this->savePath . $f[ 'name']);
if(@move_uploaded_file($f['tmp_name'],$this->savePath . mb_convert_encoding($f['name'],'gbk','utf-8'))) {//如果不進行編碼轉換,中文將無法支援
$this->log .= $f['name'] . "成功上傳到目錄:". $this->savePath ."n";
$filearray[$k] = $this->savePath . $f['name'];
}else{
$this->log .= $f['name'] . "上傳失敗。 n";
}
}
}
return $filearray;
}
/*偵測檔案的型別
*(array)$files 檔案陣列
* /
private function check_file_type($files) {
$this->filelist = array();
foreach($files as $key=>$file) {
if($file[' error'] == 0) {
$ext = strtolower(substr($file['name'], strrpos($file['name'], '.') + 1));
$str = @file_get_contents($file['tmp_name'],FALSE,NULL,0,20);
if((in_array($ext,array('jpg','jpeg')) && substr($str ,0 , 3) !== "xFFxD8xFF") || ($ext == 'gif' && substr($str ,0, 4) !== 'GIF8') || ($ext == 'png' && substr( $str ,0, 8) !== "x89x50x4Ex47x0Dx0Ax1Ax0A") || ($ext == 'bmp' && substr($str ,0, 2) !== 'BM') || ($ext == 'swf ' && (substr($str ,0, 3) !== 'CWS' || substr($str ,0, 3) !== 'FWS')) || ($ext == 'zip' && substr( $str ,0, 4) !== "PKx03x04") || ($ext == 'rar' && substr($str ,0, 4) !== 'Rar!') || ($ext == ' pdf' && substr($str ,0, 4) !== "x25PDF") || ($ext == 'chm' && substr($str ,0, 4) !== 'ITSF') || ($ ext == 'rm' && substr($str ,0, 4) !== "x2ERMF") || ($ext == 'exe' && substr($str ,0, 2) !== "MZ") || (in_array($ext,array('doc','xls','ppt')) && substr($str ,0, 4) !== "xD0xCFx11xE0")) {
$this->log .= $file['name'] . "檔案類型與檔案內容不符合。n";
}elseif((!empty($this->extensionFileFormat) && in_array($ext,$this->extensionFileFormat)) || (empty($this->extensionFileFormat) && (in_array( $ext, $this->ImageFileFormat) || in_array($ext,$this->OtherFileFormat)))) {
$this->filelist[$key] = $file
}else; ->log .= $ file['name'] . "不符合上傳檔案的型別。n"
@unlink($file['tmp_name'])
}
}
}
}
/*產生上傳目錄
*
*/
private function makeDirectory() {
$directoryName = str_replace("\", "/", $this->savePath );
$ dirNames =explode('/', $directoryName);
$total = count($dirNames);
$temp = ''; {
$temp . = $dirNames[$i].'/'
if (!is_dir($temp) ))
{
$ oldmask = @umask(0);
if (!@mkdir($ temp, 0777)) 回傳false
@umask($oldmask)
}
}; >savePath)) {
回傳true
} else {
回傳false >

以上就介紹了PHP5+UTF8多文件上傳類,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
高流量網站的PHP性能調整高流量網站的PHP性能調整May 14, 2025 am 12:13 AM

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

PHP中的依賴注入:初學者的代碼示例PHP中的依賴注入:初學者的代碼示例May 14, 2025 am 12:08 AM

你應該關心DependencyInjection(DI),因為它能讓你的代碼更清晰、更易維護。 1)DI通過解耦類,使其更模塊化,2)提高了測試的便捷性和代碼的靈活性,3)使用DI容器可以管理複雜的依賴關係,但要注意性能影響和循環依賴問題,4)最佳實踐是依賴於抽象接口,實現鬆散耦合。

PHP性能:是否可以優化應用程序?PHP性能:是否可以優化應用程序?May 14, 2025 am 12:04 AM

是的,優化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)優化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,並避免使用

PHP性能優化:最終指南PHP性能優化:最終指南May 14, 2025 am 12:02 AM

theKeyStrategiestosigantificallyBoostPhpaPplicationPerformenCeare:1)UseOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)優化AtabaseInteractionswithPreparedStateTementStatementStatementAndProperIndexing,3)配置

PHP依賴注入容器:快速啟動PHP依賴注入容器:快速啟動May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依賴注入與服務定位器PHP中的依賴注入與服務定位器May 13, 2025 am 12:10 AM

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

PHP性能優化策略。PHP性能優化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP電子郵件驗證:確保正確發送電子郵件PHP電子郵件驗證:確保正確發送電子郵件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!