新聞中心
上傳類庫
創(chuàng)新互聯(lián)專注于江安企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開發(fā),商城建設(shè)。江安網(wǎng)站建設(shè)公司,為江安等地區(qū)提供建站服務(wù)。全流程按需制作網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
class upload {
protected $allowExt = array('jpg','jpeg','gif','png','bmp');
protected $allowSize = 1; // 最大上傳大小,單位為M
protected $errno = 0;
protected $error = array(
0=>'上傳完成',
1=>'文件超出upload_max_filesize',
2=>'文件超出表單中 MAX_FILE_SIZE 選項(xiàng)指定的值',
3=>'文件只有部分被上傳',
4=>'沒有文件被上傳',
6=>'找不到臨時(shí)目錄',
7=>'文件定入失敗',
8=>'文件大小超出配置文件的限制',
9=>'不允許的文件類型',
10=>'創(chuàng)建目錄失敗',
11=>'未知錯(cuò)誤,反思中'
);
// 獲取后綴
protected function getExt($file) {
$ext = strtolower(strrchr($file,'.'));
return $ext;
}
// 檢驗(yàn)后綴
protected function checkExt($ext) {
return in_array(ltrim($ext,'.'),$this->allowExt);
}
// 檢驗(yàn)大小
protected function checkSize($size) {
return $size <= $this->allowSize * 1000 * 1000;
}
// 按日期生成目錄
protected function mk_dir() {
$dir = date('Y/m/d',time());
$dir = ROOT . 'data/p_w_picpaths/' . $dir;
if(!is_dir($dir)) {
if(!mkdir($dir,0777,true)) {
return false;
}
}
return $dir;
}
// 生成隨機(jī)文件名
protected function randName($n = 6) {
if($n <= 0) {
return '';
}
$str = 'abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ0123456789';
$str = substr(str_shuffle($str),0,$n);
return $str;
}
public function up($name) {
// $_FILES里面有沒有$name指定的單元
if(!isset($_FILES[$name])) {
return false;
}
$f = $_FILES[$name];
// 判斷錯(cuò)誤碼,是否上傳成功
if(($this->errno = $f['error']) > 0) {
return false;
}
// 判斷大小
if(!$this->checkSize($f['size'])) {
$this->errno = 8;
return false;
}
// 判斷類型
$ext = $this->getExt($f['name']);
if(!$this->checkExt($ext)) {
$this->errno = 9;
return false;
}
// 上傳,返回路徑
$path = $this->mk_dir();
if(!$path) {
$this->errno = 10;
return false;
}
$filename = $this->randName(6);
$path = $path . '/' . $filename . $ext;
if(!move_uploaded_file($f['tmp_name'],$path)) {
$this->errno = 11;
return false;
}
$path = str_replace(ROOT,'',$path);
return $path;
}
// 獲取錯(cuò)誤的接口
public function getErr() {
return $this->error[$this->errno];
}
// 設(shè)置允許的后綴
public function setExt($arr) {
$this->allowExt = $arr;
}
// 設(shè)置最大上傳值
public function setSize($num=2) {
$this->allowSize = $num;
}
}
/*
define('ROOT','D:/www/0713/');
$upload = new upload();
if($path = $upload->up('pic')) {
echo $path,'
';
echo '上傳成功';
} else {
echo $upload->getErr();
}
*/
分享標(biāo)題:微型php框架library/upload.class.php
網(wǎng)站網(wǎng)址:http://fisionsoft.com.cn/article/ihppdd.html