php+文件上传类的代码,php文件上传类程序代码

类函数:

/**

* 文件上传类

*/

class uploadFile {

public $max_size = ’1000000′;//设置上传文件大小

public $file_name = ‘date’;//重命名方式代表以时间命名,其他则使用给予的名称

public $allow_types;//允许上传的文件扩展名,不同文件类型用“|”隔开

public $errmsg = ”;//错误信息

public $uploaded = ”;//上传后的文件名(包括文件路径)

public $save_path;//上传文件保存路径

private $files;//提交的等待上传文件

private $file_type = array();//文件类型

private $ext = ”;//上传文件扩展名

/**

* 构造函数,初始化类

* @access public

* @param string $file_name 上传后的文件名

* @param string $save_path 上传的目标文件夹

*/

public function __construct($save_path = ‘./upload/’,$file_name = ‘date’,$allow_types = ”) {

$this->file_name = $file_name;//重命名方式代表以时间命名,其他则使用给予的名称

$this->save_path = (preg_match(‘//$/’,$save_path)) ? $save_path : $save_path . ‘/’;

$this->allow_types = $allow_types == ” ? ‘jpg|gif|png|zip|rar’ : $allow_types;

}

/**

* 上传文件

* @access public

* @param $files 等待上传的文件(表单传来的$_FILES[])

* @return boolean 返回布尔值

*/

public function upload_file($files) {

$name = $files['name'];

$type = $files['type'];

$size = $files['size'];

$tmp_name = $files['tmp_name'];

$error = $files['error'];

switch ($error) {

case 0 : $this->errmsg = ”;

break;

case 1 : $this->errmsg = ‘超过了php.ini中文件大小’;

break;

case 2 : $this->errmsg = ‘超过了MAX_FILE_SIZE 选项指定的文件大小’;

break;

case 3 : $this->errmsg = ‘文件只有部分被上传’;

break;

case 4 : $this->errmsg = ‘没有文件被上传’;

break;

case 5 : $this->errmsg = ‘上传文件大小为0′;

break;

default : $this->errmsg = ‘上传文件失败!’;

break;

}

if($error == 0 && is_uploaded_file($tmp_name)) {

//检测文件类型

if($this->check_file_type($name) == FALSE){

return FALSE;

}

//检测文件大小

if($size > $this->max_size){

$this->errmsg = ‘上传文件’.$name.’太大,最大支持’.ceil($this->max_size/1024).’kb的文件’;

return FALSE;

}

$this->set_save_path();//设置文件存放路径

$new_name = $this->file_name != ‘date’ ? $this->file_name.’.’.$this->ext : date(‘YmdHis’).’.’.$this->ext;//设置新文件名

$this->uploaded = $this->save_path.$new_name;//上传后的文件名

//移动文件

if(move_uploaded_file($tmp_name,$this->uploaded)){

$this->errmsg = ‘文件’.$this->uploaded.’上传成功!’;

return TRUE;

}else{

$this->errmsg = ‘文件’.$this->uploaded.’上传失败!’;

return FALSE;

}

}

}

/**

* 检查上传文件类型

* @access public

* @param string $filename 等待检查的文件名

* @return 如果检查通过返回TRUE 未通过则返回FALSE和错误消息

*/

public function check_file_type($filename){

$ext = $this->get_file_type($filename);

$this->ext = $ext;

$allow_types = explode(‘|’,$this->allow_types);//分割允许上传的文件扩展名为数组

//echo $ext;

//检查上传文件扩展名是否在请允许上传的文件扩展名中

if(in_array($ext,$allow_types)){

return TRUE;

}else{

$this->errmsg = ‘上传文件’.$filename.’类型错误,只支持上传’.str_replace(‘|’,',’,$this->allow_types).’等文件类型!’;

return FALSE;

}

}

/**

* 取得文件类型

* @access public

* @param string $filename 要取得文件类型的目标文件名

* @return string 文件类型

*/

public function get_file_type($filename){

$info = pathinfo($filename);

$ext = $info['extension'];

return $ext;

}

/**

* 设置文件上传后的保存路径

*/

public function set_save_path(){

$this->save_path = (preg_match(‘//$/’,$this->save_path)) ? $this->save_path : $this->save_path . ‘/’;

if(!is_dir($this->save_path)){

//如果目录不存在,创建目录

$this->set_dir();

}

}

/**

* 创建目录

* @access public

* @param string $dir 要创建目录的路径

* @return boolean 失败时返回错误消息和FALSE

*/

public function set_dir($dir = null){

//检查路径是否存在

if(!$dir){

$dir = $this->save_path;

}

if(is_dir($dir)){

$this->errmsg = ‘需要创建的文件夹已经存在!’;

}

$dir = explode(‘/’, $dir);

foreach($dir as $v){

if($v){

$d .= $v . ‘/’;

if(!is_dir($d)){

$state = mkdir($d, 0777);

if(!$state)

$this->errmsg = ‘在创建目录’ . $d . ‘时出错!’;

}

}

}

return true;

}

}

/*************************************************

* 图片处理类

*

* 可以对图片进行生成缩略图,打水印等操作

* 本类默认编码为UTF8 如果要在GBK下使用请将img_mark方法中打中文字符串水印iconv注释去掉

*

* 由于UTF8汉字和英文字母大小(像素)不好确定,在中英文混合出现太多时可能会出现字符串偏左

* 或偏右,请根据项目环境对get_mark_xy方法中的$strc_w = strlen($this->mark_str)*7+5进

* 行调整

* 需要GD库支持,为更好使用本类推荐使用GD库2.0+

*

* @author kickflip@php100 QQ263340607

*************************************************/

class uploadImg extends uploadFile {

public $mark_str = ‘kickflip@php100′; //水印字符串

public $str_r = 0; //字符串颜色R

public $str_g = 0; //字符串颜色G

public $str_b = 0; //字符串颜色B

public $mark_ttf = ‘./upload/SIMSUN.TTC’; //水印文字字体文件(包含路径)

public $mark_logo = ‘./upload/logo.png’; //水印图片

public $resize_h;//生成缩略图高

public $resize_w;//生成缩略图宽

public $source_img;//源图片文件

public $dst_path = ‘./upload/’;//缩略图文件存放目录,不填则为源图片存放目录

/**

* 生成缩略图 生成后的图

* @access public

* @param integer $w 缩小后图片的宽(px)

* @param integer $h 缩小后图片的高(px)

* @param string $source_img 源图片(路径+文件名)

*/

public function img_resized($w,$h,$source_img = NULL){

$source_img = $source_img == NULL ? $this->uploaded : $source_img;//取得源文件的地址,如果为空则默认为上次上传的图片

if(!is_file($source_img)) { //检查源图片是否存在

$this->errmsg = ‘文件’.$source_img.’不存在’;

return FALSE;

}

$this->source_img = $source_img;

$img_info = getimagesize($source_img);

$source = $this->img_create($source_img); //创建源图片

$this->resize_w = $w;

$this->resize_h = $h;

$thumb = imagecreatetruecolor($w,$h);

imagecopyresized($thumb,$source,0,0,0,0,$w,$h,$img_info[0],$img_info[1]);//生成缩略图片

$dst_path = $this->dst_path == ” ? $this->save_path : $this->dst_path; //取得目标文件夹路径

$dst_path = (preg_match(‘//$/’,$dst_path)) ? $dst_path : $dst_path . ‘/’;//将目标文件夹后加上/

if(!is_dir($dst_path)) $this->set_dir($dst_path); //如果不存在目标文件夹则创建

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值