適用于初學(xué)者的簡(jiǎn)易PHP文件上傳類
本文實(shí)例講述了PHP多文件上傳類,分享給大家供大家參考。具體如下:
<?php
class Test_Upload{
protected $_uploaded = array();
protected $_destination;
protected $_max = 1024000;
protected $_messages = array();
protected $_permited = array(
'image/gif',
'image/jpeg',
'image/pjpeg',
'image/png'
);
protected $_renamed = false;
/**
*
* @param mix $path
*
*/
public function __construct($path){
if (!is_dir($path) || !is_writable($path)){
throw new Exception("文件名不可寫,或者不是目錄!");
}
$this->_destination = $path;
$this->_uploaded = $_FILES;
}
/**
* 移動(dòng)文件
*
*/
public function move(){
$filed = current($this->_uploaded);
$isOk = $this->checkError($filed['name'], $filed['error']);
//debug ok
if ($isOk){
$sizeOk = $this->checkSize($filed['name'], $filed['size']);
$typeOk = $this->checkType($filed['name'], $filed['type']);
if ($sizeOk && $typeOk){
$success = move_uploaded_file($filed['tmp_name'], $this->_destination.$filed['name']);
if ($success){
$this->_messages[] = $filed['name']."文件上傳成功";
}else {
$this->_messages[] = $filed['name']."文件上傳失敗";
}
}
}
}
/**
* 查詢messages數(shù)組內(nèi)容
*
*/
public function getMessages(){
return $this->_messages;
}
/**
* 檢測(cè)上傳的文件大小
* @param mix $string
* @param int $size
*/
public function checkSize($filename, $size){
if ($size == 0){
return false;
}else if ($size > $this->_max){
$this->_messages[] = "文件超出上傳限制大小".$this->getMaxsize();
return false;
}else {
return true;
}
}
/**
* 檢測(cè)上傳文件的類型
* @param mix $filename
* @param mix $type
*/
protected function checkType($filename, $type){
if (!in_array($type, $this->_permited)){
$this->_messages[] = "該文件類型是不被允許的上傳類型";
return false;
}else {
return true;
}
}
/**
* 獲取文件大小
*
*/
public function getMaxsize(){
return number_format($this->_max / 1024, 1).'KB';
}
/**
* 檢測(cè)上傳錯(cuò)誤
* @param mix $filename
* @param int $error
*
*/
public function checkError($filename, $error){
switch ($error){
case 0 : return true;
case 1 :
case 2 : $this->_messages[] = "文件過大!"; return true;
case 3 : $this->_messages[] = "錯(cuò)誤上傳文件!";return false;
case 4 : $this->_messages[] = "沒有選擇文件!"; return false;
default : $this->_messages[] = "系統(tǒng)錯(cuò)誤!"; return false;
}
}
}
?>
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- 一個(gè)經(jīng)典的PHP文件上傳類分享
- php判斷文件上傳類型及過濾不安全數(shù)據(jù)的方法
- PHP5+UTF8多文件上傳類
- php可生成縮略圖的文件上傳類實(shí)例
- 非常經(jīng)典的PHP文件上傳類分享
- 功能強(qiáng)大的php文件上傳類
- PHP多文件上傳類實(shí)例
- php文件上傳類完整實(shí)例
- PHP實(shí)現(xiàn)的多文件上傳類及用法示例
- php+ajax實(shí)現(xiàn)圖片文件上傳功能實(shí)例
- 使用ajaxfileupload.js實(shí)現(xiàn)ajax上傳文件php版
- PHP實(shí)現(xiàn)的文件上傳類與用法詳解
相關(guān)文章
thinkPHP5實(shí)現(xiàn)的查詢數(shù)據(jù)庫(kù)并返回json數(shù)據(jù)實(shí)例
這篇文章主要介紹了thinkPHP5實(shí)現(xiàn)的查詢數(shù)據(jù)庫(kù)并返回json數(shù)據(jù)功能,結(jié)合實(shí)例形式分析了thinkPHP5數(shù)據(jù)庫(kù)查詢及json格式數(shù)據(jù)簡(jiǎn)單操作技巧,需要的朋友可以參考下2017-10-10
PHP開發(fā)者常犯的10個(gè)MySQL錯(cuò)誤更正剖析
最近看到一篇文章:《PHP開發(fā)者常犯的10個(gè)MySQL錯(cuò)誤》,發(fā)現(xiàn)文中不少內(nèi)容陳舊,隨著時(shí)間推移技術(shù)發(fā)展變化而變得不適用。為了防止誤導(dǎo)新手,特本著與時(shí)俱進(jìn)的精神寫出此文,絕非對(duì)原文作者的不尊重2012-01-01
PHP基于ICU擴(kuò)展intl快速實(shí)現(xiàn)漢字轉(zhuǎn)拼音及按拼音首字母分組排序的方法
這篇文章主要介紹了PHP基于ICU擴(kuò)展intl快速實(shí)現(xiàn)漢字轉(zhuǎn)拼音及按拼音首字母分組排序的方法,結(jié)合實(shí)例形式分析了ICU擴(kuò)展intl的實(shí)現(xiàn)方法與拼音轉(zhuǎn)換、排序等相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
php圖像處理函數(shù)imagecopyresampled用法詳解
這篇文章主要介紹了php圖像處理函數(shù)imagecopyresampled用法,結(jié)合實(shí)例形式詳細(xì)分析了imagecopyresampled函數(shù)的功能、參數(shù)、使用方法,需要的朋友可以參考下2016-12-12
centos7環(huán)境下swoole1.9的安裝與HttpServer的使用方法分析
這篇文章主要介紹了centos7環(huán)境下swoole1.9的安裝與HttpServer的使用方法,結(jié)合實(shí)例形式分析了centos7環(huán)境下swoole1.9的安裝、配置方法以及HttpServer的相關(guān)使用技巧,需要的朋友可以參考下2020-03-03
PHP+Ajax實(shí)時(shí)自動(dòng)檢測(cè)是否聯(lián)網(wǎng)的方法
這篇文章主要介紹了PHP+Ajax實(shí)時(shí)自動(dòng)檢測(cè)是否聯(lián)網(wǎng)的方法,通過Ajax調(diào)用連接百度效果實(shí)現(xiàn)檢測(cè)網(wǎng)站是否聯(lián)網(wǎng)的功能,需要的朋友可以參考下2015-07-07

