upload.php
define('IN_WEI',true);
define('ROOT', dirname(__FILE__).DIRECTORY_SEPARATOR);
require('uptool.class.php');
$uptool= new uptool();
if($uptool->upLoad('pic')){
echo 'upload success';
}else{
echo 'upload fail
';
echo $uptool->showError();
}
uptool.class.php
defined('IN_WEI') or exit('IN_WEI denied');
//单文件上传类
class uptool {
public $allowExt = 'jpg,jpeg,gif,bmp,png';
public $maxSize = 1;//单位为M
protected $errornum = 0;
protected $error =array(
0=>'没有错误',
1=>'上传文件大小超出PHP限制',
2=>'上传文件大小抄书页面表单限制',
3=>'文件只有部分上传',
4=>'没有文件被上传',
6=>'找不到临时文件夹',
7=>'文件写入失败',
8=>'超出自定义后缀',
9=>'超出自定义大小',
10=>'创建目录失败',
11=>'随机名称创建失败',
12=>'文件移动失败'
);
public function upLoad($filename){
$file=$_FILES[$filename];
if(!isset($file)){
$this->errornum = $file['error'];
return false;
}
if($file['error']){
$this->errornum = $file['error'];
return false;
}
$ext=$this->getExt($file);
$size=$this->getSize($file);
if(!$this->isAllowExt($ext)){
$this->errornum = 8;
return false;
}
if(!$this->isAllowSize($size)){
$this->errornum = 9;
return false;
}
if(!$dir=$this->makeDir()){
$this->errornum = 10;
return false;
}
if(!$name=$this->makeName()){
$this->errornum = 11;
return false;
}
$dirname=$dir.'/'.$name.'.'.$ext;
if(!move_uploaded_file($file['tmp_name'], $dirname)){
$this->errornum = 12;
return false;
}
return $dirname;
}
public function showError(){
return $this->error[$this->errornum];
}
protected function getExt($file){
return end(explode('.', $file['name']));
}
protected function getSize($file){
return $file['size'];
}
protected function isAllowSize($size){
if($size <= $this->maxSize*1024*1024){
return true;
}else{
return false;
}
}
protected function isAllowExt($ext){
return in_array(strtolower($ext),explode(',', $this->allowExt));
}
protected function makeDir(){
$dir='./upload/'.date('Y/m/d',time());
if(is_dir($dir)||mkdir($dir,0777,true)){
return $dir;
}else{
return false;
}
}
protected function makeName($length = 6){
$str="abcdefghigklmnopqrstuvwxyz23456789";
return substr(str_shuffle($str),0,$length);
}
}
file.html
html>
fileupload姓名:
头像1: