php编写api单文件入口类,PHP_单文件上传简单类

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:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值