javascript和JQuery焦点图和代码特效大全
当前最流行的开源CMS网站系统大全
当前位置:主页 > 编程开发 > PHP编程 >

PHP上传类-upload.class.php 实现文件上传

来源:IT技术网编辑:素还真发布于:2013-06-29人围观Php上传upload文件上传

PHP上传类-upload.class.php 实现文件上传,使用方法:
$file = new upload;
$file->uploadFile('face', rand().'/');
第二个参数是用来灵活定义存放目录的(比如可以给每一个用户按照用户UID来存放),当然可默认为空(存放地址就按照配置方法中的地址)

 

<?php
 
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().'/');

Php,上传,upload,相关的文章
有时间的话来看看IT界的突发事件