PHP5+UTF8多文件上傳類
還有些功能沒有加上去,如自動(dòng)更名,圖片處理等.可根據(jù)需要自己添加.
USE:
$up = new upfile(ROOT_PATH.'data/'.date("Ym",time()),array('gif','jpg','jpeg'),true);
$fileimg = $up->upload($_FILES['img']);//返回上傳后文件名數(shù)組,$_FILES['img']為上傳的文件
可使用$up->log查看上傳時(shí)信息.
<?php
//====================================================
// 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; # 同名時(shí)是否覆蓋
private $maxSize = 0; # 文件最大字節(jié),為0時(shí)不限制大小
private $ext;
private $errno = 0;
/* 構(gòu)造函數(shù)
* (string)$savePath 文件保存路徑,默認(rèn)為$attachment_path
* (array)$extensionFileFormat 自定義上傳文件的擴(kuò)展名,未設(shè)置時(shí)為$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:array();
$this->overwrite = is_bool($overwrite)?$overwrite:false;
}
/*上傳函數(shù)
* (array)$files 待上傳的文件數(shù)組$_FILES['attach']
* (number)$maxSize 文件的最大字節(jié)數(shù),默認(rèn)為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($this->filelist)) {
$this->log .= "待上傳的文件列表為空。\n";
return array();
}
if(!self::makeDirectory() || !@is_writable($this->savePath)) {
$this->log .= $this->savePath . "不能創(chuàng)建或其權(quán)限為不可寫。\n";
return array();
}
$filearray = array();
foreach($this->filelist as $k=>$f) {
if($this->maxSize && $f['size']>$this->maxSize) {
$this->log .= $f['name'] . "其大小超過了設(shè)定的值:" . $this->maxSize ."\n";
}elseif($this->overwrite == false && file_exists($this->savePath . $f['name'])) {
$this->log .= $f['name'] . "已經(jīng)存在于目錄:" . $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'))) {//如果不進(jìn)行編碼轉(zhuǎn)換,中文將無(wú)法支持
$this->log .= $f['name'] . "成功上傳到目錄:". $this->savePath ."\n";
$filearray[$k] = $this->savePath . $f['name'];
}else{
$this->log .= $f['name'] . "上傳失敗。\n";
}
}
}
return $filearray;
}
/*檢測(cè)文件的類型
*(array)$files 文件數(shù)組
*/
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) !== "\xFF\xD8\xFF") || ($ext == 'gif' && substr($str ,0, 4) !== 'GIF8') || ($ext == 'png' && substr($str ,0, 8) !== "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A") || ($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) !== "PK\x03\x04") || ($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) !== "\xD0\xCF\x11\xE0")) {
$this->log .= $file['name'] . "文件類型與文件內(nèi)容不符合。\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{
$this->log .= $file['name'] . "不符合上傳文件的類型。\n";
@unlink($file['tmp_name']);
}
}
}
}
/*生成上傳目錄
*
*/
private function makeDirectory() {
$directoryName = str_replace("\\","/", $this->savePath);
$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)) return false;
@umask($oldmask);
}
};
if(is_dir($this->savePath)) {
return true;
} else {
return false;
};
}
}
?>
相關(guān)文章
PHP 日期時(shí)間函數(shù)的高級(jí)應(yīng)用技巧
PHP 日期時(shí)間函數(shù)常用函數(shù)高級(jí)使用技巧,大家在以后的開發(fā)中能用的到。2009-10-10
PHP實(shí)現(xiàn)的抓取小說網(wǎng)站內(nèi)容功能示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的抓取小說網(wǎng)站內(nèi)容功能,涉及php頁(yè)面抓取、正則匹配、文件讀寫等相關(guān)操作技巧,需要的朋友可以參考下2019-06-06
PHP For循環(huán)字母A-Z當(dāng)超過26個(gè)字母時(shí)輸出AA,AB,AC
這篇文章主要介紹了PHP For循環(huán)字母A-Z當(dāng)超過26個(gè)字母時(shí)輸出AA,AB,AC,需要的朋友可以參考下2020-02-02
Referer原理與圖片防盜鏈實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Referer原理與圖片防盜鏈實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Referer頭信息原理與圖片防盜鏈判定、實(shí)現(xiàn)方法,并附帶一個(gè)Http請(qǐng)求封裝類,需要的朋友可以參考下2019-07-07
原生PHP實(shí)現(xiàn)導(dǎo)出csv格式Excel文件的方法示例【附源碼下載】
這篇文章主要介紹了原生PHP實(shí)現(xiàn)導(dǎo)出csv格式Excel文件的方法,結(jié)合實(shí)例形式分析了基于原生php實(shí)現(xiàn)的Excel文件操作類進(jìn)行Excel文件的導(dǎo)出操作相關(guān)實(shí)現(xiàn)技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2019-03-03
簡(jiǎn)單的php 驗(yàn)證圖片生成函數(shù)
本函數(shù)可以生成簡(jiǎn)單的驗(yàn)證圖片,用于輸入的驗(yàn)證。2009-05-05
詳解PHP中互斥鎖庫(kù)hyperf-wise-locksmith的使用
hyperf-wise-locksmith?庫(kù)作為?Hyperf?框架中的一員,提供了一個(gè)高效、簡(jiǎn)潔的互斥鎖解決方案,下面就跟隨小編一起了解下它的具體使用吧2024-11-11

