最新的php 文件上傳模型,支持多文件上傳
更新時(shí)間:2009年08月13日 14:33:23 作者:
為 MayFish 重新封裝的一個(gè)文件上傳類,支持多個(gè)文件同時(shí)上傳,設(shè)置允許的上傳文件類型和文件大小。
復(fù)制代碼 代碼如下:
<?php
class UploadModel
{
protected $keys;
protected $err = array();
protected $target;
protected $exts;
protected $maxSize;
protected $randName;
protected $files = array();
/**
* 初始化變量
*/
public function __construct()
{
$this->exts = array('jpeg','jpg','gif','png','zip','rar');
$this->maxSize = 1024*1024*2;
$this->target = dirname(__FILE__) . '/upload/';
$this->randName = true;
$this->keys = $this->getKeys();
}
/**
* 獲取 file 的名稱
*/
protected function getKeys()
{
$keys = array_keys($_FILES);
return $keys;
}
/**
* 設(shè)置不同類型的變量
*/
public function __set($name, $value)
{
$property = array('target','exts','maxSize','randName');
if(!in_array($name, $property)) return false;
switch(strval($name))
{
case 'target':
$this->$name = Configure::read('app_path') . $value;
break;
case 'exts':
$this->$name = explode(',', $value);
break;
case 'randName':
if($value === true || $value == 1)
{
$this->$name = true;
}
else {
$this->$name = false;
}
break;
default:
$this->$name = $value;
}
}
/**
* 移動(dòng)上傳的文件到指定的目錄
* @param $fileName 移動(dòng)單個(gè)文件名稱的時(shí)候,對(duì)上傳的文件重新命名
*/
public function save($fileName)
{
$this->err = array();
$this->files = array();
if(!file_exists($this->target)) {
mkdir($this->target);
chmod($this->target, 0777);
}
foreach($this->keys as $key)
{
if(is_array($_FILES[$key]['name']))
{
$count = count($_FILES[$key]['name']);
for($i=0; $i<$count; $i++)
{
$keys = array_keys($_FILES[$key]);
foreach($keys as $item)
{
$file[$item] = $_FILES[$key][$item][$i];
}
$this->upload($file, $fileName);
}
return (count($this->err) > 0)? false:true;
}
else {
return $this->upload($_FILE[$key], $fileName);
}
}
}
/** 內(nèi)部處理上傳文件的過(guò)程 */
protected function upload($file, $fileName)
{
if(!is_uploaded_file($file['tmp_name'])) return false;
if(!$this->checkExt($file)) return false;
if(!$this->checkSize($file)) return false;
if($this->randName)
{
$newFileName = $this->target . date('YmdHis', time()) . rand(0,9) . '.' . $this->getExt($file['name']);
}
elseif(emptyempty($fileName))
{
$newFileName = $this->target . '/' . $file['name'];
}
else {
$newFileName = $this->target . '/' . $fileName;
}
$result = move_uploaded_file($file['tmp_name'], $newFileName);
if(!$result)
{
return false;
}
else {
$this->files[] = str_replace($this->target, $newFileName);
return true;
}
}
/**
* 是否是可上傳的文件類型
* @param $file 文件對(duì)象
*/
public function checkExt($file)
{
if(!in_array($this->getExt($file['name']), $this->exts))
{
$this->err[] = $file['name'].':ext';
return false;
}
else {
return true;
}
}
/**
* 獲取文件后綴名
*/
public function getExt($fileName)
{
$pos = strrpos($fileName, '.')+1;
return substr($fileName, $pos);
}
/**
* 檢查文件大小是否合法
*/
public function checkSize($file)
{
if($size > $this->maxSize)
{
$this->err[] = $file['name'].':max';
return false;
}
else {
return true;
}
}
/**
* 取得已經(jīng)上傳的文件名稱
*/
public function getFiles()
{
return $this->files;
}
}
使用實(shí)例:
復(fù)制代碼 代碼如下:
include 'uploaded.model.php';
$U = new UploadModel();
$U->target = '/tmp/';
$U->exts = 'jpg,gif';
$U->maxSize = 1024*275; //275KB
$U->save();
$files = $U->getFiles();
print_r($files);
include 'uploaded.model.php';
$U = new UploadModel();
$U->target = '/tmp/';
$U->exts = 'jpg,gif';
$U->maxSize = 1024*275; //275KB
$U->save();
$files = $U->getFiles();
print_r($files);
在 MayFish 里的使用實(shí)例:
復(fù)制代碼 代碼如下:
public function up()
{
$U = M('SYS', 'upload');
$U->target = '/tmp/';
$U->exts = 'jpg,gif';
$U->maxSize = 1024*275; //275KB
$U->save();
header('Location:/?a=upload');
}
public function up()
{
$U = M('SYS', 'upload');
$U->target = '/tmp/';
$U->exts = 'jpg,gif';
$U->maxSize = 1024*275; //275KB
$U->save();
header('Location:/?a=upload');
}
前臺(tái)代碼:
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無(wú)標(biāo)題文檔</title>
</head>
<body>
<form action="/?a=up" method="post" enctype="multipart/form-data">
<!-- 以下兩上file類型控制的name屬性可以任意設(shè)置,系統(tǒng)會(huì)自己取出input 的名稱 -->
<input name="files[]" type="file" size="30" />
<input name="files[]" type="file" size="30" />
<input type="submit" value="開(kāi)始上傳" />
</form>
</body>
</html>
您可能感興趣的文章:
- ThinkPHP關(guān)聯(lián)模型操作實(shí)例分析
- PHP實(shí)現(xiàn)MVC開(kāi)發(fā)得最簡(jiǎn)單的方法——模型
- ThinkPHP實(shí)例化模型的四種方法概述
- thinkphp視圖模型查詢提示ERR: 1146:Table ''db.pr_order_view'' doesn''t exist的解決方法
- PHP開(kāi)發(fā)框架Laravel數(shù)據(jù)庫(kù)操作方法總結(jié)
- PHP的Yii框架中使用數(shù)據(jù)庫(kù)的配置和SQL操作實(shí)例教程
- PHP的Laravel框架結(jié)合MySQL與Redis數(shù)據(jù)庫(kù)的使用部署
- 實(shí)現(xiàn)PHP框架系列文章(6)mysql數(shù)據(jù)庫(kù)方法
- ThinkPHP框架分布式數(shù)據(jù)庫(kù)連接方法詳解
- 自制PHP框架之模型與數(shù)據(jù)庫(kù)
相關(guān)文章
php實(shí)現(xiàn)在服務(wù)器端調(diào)整圖片大小的方法
這篇文章主要介紹了php實(shí)現(xiàn)在服務(wù)器端調(diào)整圖片大小的方法,實(shí)例分析了imageResizer與loadimage操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-06-06
phpStudy在運(yùn)行PHP文件時(shí)出現(xiàn)中文亂碼的有效解決方法
在使用phpStudy進(jìn)行PHP開(kāi)發(fā)時(shí),經(jīng)常會(huì)遇到中文顯示亂碼的問(wèn)題,這不僅影響開(kāi)發(fā)效率,還可能導(dǎo)致網(wǎng)站顯示異常,本文將詳細(xì)介紹解決phpStudy中文亂碼的有效方法,需要的朋友可以參考下2024-10-10
晉城吧對(duì)DiscuzX進(jìn)行的前端優(yōu)化要點(diǎn)
晉城吧的服務(wù)器在美國(guó),延遲相對(duì)國(guó)內(nèi)略微要高一些,所以優(yōu)化就顯得非常重要。2010-09-09
php實(shí)現(xiàn)數(shù)據(jù)庫(kù)的增刪改查
本文給大家介紹的是PHP連接數(shù)據(jù)庫(kù)以及實(shí)現(xiàn)數(shù)據(jù)庫(kù)的增刪改查功能的方法及示例代碼,希望對(duì)大家學(xué)習(xí)php能夠有所幫助2017-02-02
php如何調(diào)用webservice應(yīng)用介紹
Web Service也叫XML Web Service WebService是一種可以接收從Internet或者Intranet上的其它系統(tǒng)中傳遞過(guò)來(lái)的請(qǐng)求,輕量級(jí)通訊技術(shù),接下來(lái)將詳細(xì)介紹php如何調(diào)用webservice,需要的朋友可以了解下2012-11-11
php實(shí)現(xiàn)異步數(shù)據(jù)調(diào)用的方法
這篇文章主要介紹了php實(shí)現(xiàn)異步數(shù)據(jù)調(diào)用的方法,分享了4種PHP異步執(zhí)行的常用方式,感興趣的小伙伴們可以參考一下2015-12-12
Optimizer與Debugger兼容性問(wèn)題的解決方法
網(wǎng)上許多聲音說(shuō)Optimizer與Debugger有沖突,不能同時(shí)開(kāi)。其實(shí)是可以的,他們兩個(gè)都是Zend擴(kuò)展插件,兩個(gè)單獨(dú)安裝都可以使用,但是要同時(shí)安裝的話必須使用一個(gè)管理器2008-12-12
php判斷某個(gè)方法是否存在函數(shù)function_exists (),method_exists()與is_callabl
這篇文章主要介紹了php判斷某個(gè)方法是否存在函數(shù)function_exists (),method_exists()與is_callable()區(qū)別與用法,結(jié)合實(shí)例形式分析了php function_exists (),method_exists()與is_callable()基本功能、用法、區(qū)別與操作注意事項(xiàng),需要的朋友可以參考下2020-04-04

