upload.class.php,php简单上传类 |upload.class.php

使用方法:

$file = new upload;

$file->uploadFile('face', rand().'/');

第二个参数是用来灵活定义存放目录的(比如可以给每一个用户按照用户UID来存放),当然可默认为空(存放地址就按照配置方法中的地址)

header('Content-Type:text/html; charset=utf-8');

class upload{

private function conf($key){

$conf = array(

'size' => 2000000,

'type' => 'jpg|jpeg|gif|png',

'path' => './images/face/',

'rename' => uniqid().gmdate('YmdHis')

);

return $conf[$key];

}

private function filext($name){

return strrchr($name, '.');

}

private function direname($rename){

$dirname = dirname($rename);

if(is_dir($dirname)){

return $rename;

}else{

@mkdir($dirname, 0755, true);

return $rename;

}

}

public function uploadFile($fileName, $userDiyPath = ''){

$tmp = $_FILES[$fileName];

if(is_uploaded_file($tmp['tmp_name'])){

if(preg_match('/'.$this->conf('type').'/i', $tmp['type'])){

if($tmp['size'] < $this->conf('size')){

if($tmp['error'] == 0){

$rename = $this->direname($this->conf('path').$userDiyPath.$this->conf('rename').$this->filext($tmp['name']));

if(@move_uploaded_file($tmp['tmp_name'], $rename)){

return $rename;

}else{

js::reload('文件上传出现错误!');

}

}else{

js::reload('文件上传出现错误!');

}

}else{

js::reload('文件上传大小错误!');

}

}else{

js::reload('文件上传类型错误!');

}

}else{

js::reload('文件上传方式错误!');

}

}

}

class js{

public static function reload($msg){

exit($msg);

}

}

$file = new upload;

$file->uploadFile('face', rand().'/');

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值