分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

<小田吃饺子> PHP:图片上传

发布时间:2023-09-06 01:13责任编辑:沈小雨关键词:PHP
<?phpclass TL_Update_File{ ???private $file = null;//接受图片名称 ???private $files = [];//图片属性 ???public $size = null;//上传文件大小限制 ???public $save_path = null;//保存路径 ???public $mime = null;//文件上传支持类型 ???private $error = null;//图片不符合要求者 ???//接受图片 ???function __construct($file=null){ ???????$this->save_path = str_replace("\\","/",dirname(__FILE__))."/"; ???????$this->file = $file; ???????if($file){ ???????????$this->files = $_FILES[$file]; ???????}else{ ???????????$this->files = $_FILES; ???????} ???} ???//一系列属性向 ???private function attribute(){ ???????if($this->file){ ???????????$this->empty_ture(); ???????}else{ ???????????$this->empty_false(); ???????} ???????if($this->size){ ???????????if($this->file){ ???????????????$this->Size_ture(); ???????????}else{ ???????????????$this->Size_false(); ???????????} ???????} ???????if($this->mime){ ???????????if($this->file){ ???????????????$this->mime_ture(); ???????????}else{ ???????????????$this->mime_false(); ???????????} ???????} ???} ???//判断图片是否为空 ???function empty_false(){ ???????foreach ($this->files as $key => $value){ ???????????if($this->files[$key][‘error‘]){ ???????????????$this->error[$key] = $value; ???????????????$this->error[$key][‘tl_error‘] = "改图片为空"; ???????????????unset($this->files[$key]); ???????????}else{ ???????????????$this->files[$key][‘mime‘] = substr($value[‘name‘], strrpos($value[‘name‘], ".")+1); ???????????} ???????} ???} ???//判断图片是否为空 ???function empty_ture(){ ???????if($this->files[‘error‘]) { ???????????$this->error[$this->file] = $this->files; ???????????$this->error[$this->file][‘tl_error‘] = "改图片为空"; ???????????$this->files=[]; ???????}else{ ???????????$this->files[‘mime‘] = substr($this->files[‘name‘], strrpos($this->files[‘name‘], ".") + 1); ???????} ???} ???//文件后缀名是否受限制 ???function mime_ture(){ ???????if($this->files){ ???????????if(!in_array($this->files[‘mime‘],$this->mime)){ ???????????????$this->error[$this->file] = $this->files; ???????????????$this->error[$this->file][‘tl_error‘] = "不支持改后缀名"; ???????????????$this->files=[]; ???????????} ???????} ???} ???//文件后缀名是否受限制 ???function mime_false(){ ???????foreach ($this->files as $key => $value){ ???????????if(!in_array($this->files[$key][‘mime‘],$this->mime)){ ???????????????$this->error[$key] = $value; ???????????????$this->error[$key][‘tl_error‘] = "不支持改后缀名"; ???????????????unset($this->files[$key]); ???????????} ???????} ???} ???//文件大小是否受限制 ???function Size_ture(){ ???????if($this->files[‘size‘] > $this->size){ ???????????$this->error[$this->file] = $this->files; ???????????$this->error[$this->file] = $this->files; ???????????$this->error[$this->file][‘tl_error‘] = "文件过大"; ???????????$this->files=[]; ???????} ???} ???//文件大小是否受限制 ???function Size_false(){ ???????foreach ($this->files as $key => $value){ ???????????if($this->files[$key][‘size‘] > $this->size){ ???????????????$this->error[$key] = $value; ???????????????$this->error[$key][‘tl_error‘] = "文件过大"; ???????????????unset($this->files[$key]); ???????????} ???????} ???} ???//保存图片 ???function save_file(){ ???????if($this->files){ ???????????$file_path_dir = date(‘Ym‘,time()) ."/"; ???????????$file_name = time() . rand(00000,99999) . "." . $this->files["mime"]; ???????????$this->is_dir_on_off($this->save_path . date(‘Ym‘,time()) ."/"); ???????????if($this->file){ ???????????????$this->save_file_ture($file_path_dir,$file_name); ???????????}else{ ???????????????$this->save_file_false($file_path_dir,$file_name); ???????????} ???????} ???} ???//保存图片 ???function save_file_ture($file_path_dir,$file_name){ ???????move_uploaded_file($this->files["tmp_name"] , $this->save_path . $file_path_dir . $file_name); ???} ???//保存图片 ???function save_file_false($file_path_dir,$file_name){ ???????foreach ($this->files as $key => $value){ ???????????move_uploaded_file($this->files[$key]["tmp_name"] , $this->save_path . $file_path_dir . $file_name); ???????} ???} ???//创建目录 ???function is_dir_on_off($file_path){ ???????if(!is_dir($file_path)){ ???????????mkdir($file_path,0777); ???????} ???} ???//保存文件 ???function move(){ ???????$this->attribute(); ???????$this->save_file(); ???}}

  

<小田吃饺子> PHP:图片上传

原文地址:http://www.cnblogs.com/hello-tl/p/7593033.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved