日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

今天写的上传类,纯练手之作,供新人学习

發布時間:2024/8/24 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 今天写的上传类,纯练手之作,供新人学习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/*
*@文件上傳
*@要獲取上傳實例 請使用 Upload::getInstance(); 具體使用方法請參考類接口
*@auther 張文慶
*@email 812035863@qq.com
*@blog www.92zyw.com
* */
class Upload{

private static $instance = null;

protected $destDir; //上傳文件的存放目錄
protected $allowType; //允許上傳的類型
protected $denyType; //拒絕上傳文件的類型
protected $mutilUpload; //是否允許多文件上傳
protected $maxUpload; //最大上傳字節數

private function __construct(array $options=null){
$this->destDir = 'upload';/* str_replace('\\','/',strstr(getcwd(),'test',true)); */
$this->allowType = array('jpg','gif');
$this->denyType = array('php');
$this->mutilUpload=false;
$this->maxUpload = '202800';
is_dir($this->destDir) or mkdir($this->destDir);
if(isset($options)){
foreach($options as $k =>$v){
$this->$k = $v;
}
}
}
//單例模式
public static function getInstance(array $options=null){
if(static::$instance == null){
if(isset($options)){
static::$instance = new Upload($options);
}else{
static::$instance = new Upload();
}
}
return static::$instance;
}
/* *
* 上傳單個文件
* @param array
*
* */
public function upload(array $file){
try {
return $this->move($file['name'],$file['tmp_name']);
} catch (UploadException $e) {
ExceptionPage::showMsg($e.getMessage(),$e->getErrno);
}

}
/*
* @多文件上傳
* @接收的參數是數組 格式 如: array(array('name'=>$file['name],'tmp_name'=>$file['tmp_file']))
*
* */
public function mutilUpload(array $files){
if(!$this->uploadAvailable()){
throw new UploadException("不允許進行多文件上傳");
return false;
}
foreach($files as $file){
if(!isset($file) && !is_array($file)){
throw new UploadException('請確認數組參數是否符合多文件上傳的要求!');
}
$this->move($file['name'],$file['tmp_name']);
}
return true;
}
/* @復制文件
* @param string
* @param string
* */
protected function move($file,$tmp_name){
$filename = $this->destDir.$file;
$fileTmpName = date('Ymdhis',time()).'.'.pathinfo($file)['extension'];
$result = move_uploaded_file($tmp_name,"{$this->destDir}/{$fileTmpName}");
if(!result){
throw new UploadException('文件不存在或者是由于一些不可預知的原因而無法移動文件');
}
return $result;
}

protected function uploadAvailable($file){
$size = filesize($file);
if($size >$this->maxUpload){
return false;
}
return false;
}
}

/* 異常類 */
class Expection{
protected $msg;
protected $errno;

public function getMessage(){
return $this->msg;
}
public function getErrno(){
return $this->errno;
}
}

/* 上傳異常類 */
class UploadExpection extends Expection{
public function __construct($msg,$errno=null){
$this->msg = $msg;
if(isset($errno)){
$this->errno =null;
}else{
$this->errno = $errno;
}
}
}
class ExceptionPage{
static $instance = null;
public $html='';
public function __construct($msg,$errno=''){

$html .='<html en="en-Ch">';
$html .='<head>';
$html .='<title>異常頁面</title>';
$html .='</head>';
$html .='<body>';
$html .="<div><p>異常:{$msg}</p>";
if(isset($errno)){
$html .='<p>異常代碼:{$errno}</p>';
}
$html .='</div>';
$html .='</body>';
$html .='</html>';
echo $html;
}
public static function showMsg($msg,$errno){
new Exception($msg,$errno);
}
}

轉載于:https://www.cnblogs.com/Super-Man/p/4218865.html

總結

以上是生活随笔為你收集整理的今天写的上传类,纯练手之作,供新人学习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。