最新的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í)候,對上傳的文件重新命名
*/
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)部處理上傳文件的過程 */
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 文件對象
*/
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>無標(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="開始上傳" />
</form>
</body>
</html>
您可能感興趣的文章:
- ThinkPHP關(guān)聯(lián)模型操作實(shí)例分析
- PHP實(shí)現(xiàn)MVC開發(fā)得最簡單的方法——模型
- ThinkPHP實(shí)例化模型的四種方法概述
- thinkphp視圖模型查詢提示ERR: 1146:Table ''db.pr_order_view'' doesn''t exist的解決方法
- PHP開發(fā)框架Laravel數(shù)據(jù)庫操作方法總結(jié)
- PHP的Yii框架中使用數(shù)據(jù)庫的配置和SQL操作實(shí)例教程
- PHP的Laravel框架結(jié)合MySQL與Redis數(shù)據(jù)庫的使用部署
- 實(shí)現(xiàn)PHP框架系列文章(6)mysql數(shù)據(jù)庫方法
- ThinkPHP框架分布式數(shù)據(jù)庫連接方法詳解
- 自制PHP框架之模型與數(shù)據(jù)庫
相關(guān)文章
PHP使用GETDATE獲取當(dāng)前日期時(shí)間作為一個(gè)關(guān)聯(lián)數(shù)組的方法
這篇文章主要介紹了PHP使用GETDATE獲取當(dāng)前日期時(shí)間作為一個(gè)關(guān)聯(lián)數(shù)組的方法,實(shí)例分析了php中GETDATE函數(shù)使用技巧,需要的朋友可以參考下2015-03-03PHP 內(nèi)存緩存加速功能memcached安裝與用法
memcached 簡介在很多場合,我們都會(huì)聽到 memcached 這個(gè)名字,但很多同學(xué)只是聽過,并沒有用過或?qū)嶋H了解過,只知道它是一個(gè)很不錯(cuò)的東東。這里簡單介紹一下。2009-09-09探討PHP中this,self,parent的區(qū)別詳解
本篇文章是對PHP中this,self,parent的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06