侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

PHP实现多图上传(结合uploadify插件)思路分析

php  /  管理员 发布于 7年前   205

本文实例讲述了PHP实现多图上传的方法。分享给大家供大家参考,具体如下:

1.已有图片可以删除
2.有一个新增的按钮
3.点击新增按钮,跳出frame框
4.在frame框中实现图片异步上传与及时效果
5.上传成功后,调用回调函数
6.弹出框中的图片可以进行删除
7.frame弹出框点击保存,把图片通过js展示到页面中
8.页面点击保存,把图片数据保存到数据库

  

删除

  

点评:点击的时候,调用GetUploadify方法。

/* * 上传图片 后台专用 * @access public * @null int 一次上传图片张图 * @elementid string 上传成功后返回路径插入指定ID元素内 * @path string 指定上传保存文件夹,默认存在Public/upload/temp/目录 * @callback string 回调函数(单张图片返回保存路径字符串,多张则为路径数组 ) */function GetUploadify(num,elementid,path,callback){    var pc_hash = $('#pc_hash').val();  var upurl ='?m=admin&c=upload&a=upload&num='+num+'&input='+elementid+'&path='+path+'&func='+callback+'&pc_hash='+pc_hash;  var iframe_str='';           $("body").append(iframe_str);    $("iframe.uploadframe").css("height",$(document).height()).css("width","100%").css("position","absolute").css("left","0px").css("top","0px").css("z-index","999999").show();  $(window).resize(function(){    $("iframe.uploadframe").css("height",$(document).height()).show();  });}

点评:生成一个iframe框。

public function upload(){    $func = $_REQUEST['func'];    $path = $_REQUEST['path'] ? $_REQUEST['path'] :'temp';    $info = array(      'num'=> $_REQUEST['num'],      'title' => '',           'upload' =>'?m=admin&c=upload&a=imageUp&savepath='.$path.'&pictitle=banner&dir=images&pc_hash='.$_SESSION['pc_hash'],      'size' => '4M',      'type' =>'jpg,png,gif,jpeg',      'input' => $_REQUEST['input'],      'func' => empty($func) ? 'undefined' : $func,    );    include $this->admin_tpl('upload_upload');}
Uploadify

最多上传个附件,单文件最大,类型

列表
 

点评:调用uploadify插件

$(function() {  $('#uploadify').uploadify({      'auto'      : true,      'method'     : 'post',      'multi'      : true,      'swf'       : 'uploadify/uploadify.swf',      'uploader'    : '',      'progressData'  : 'all',      'queueSizeLimit' : '',      'uploadLimit'   : 5,      'fileSizeLimit'  : '20000KB',      'fileTypeDesc'  : 'Image Files',      'fileTypeExts'  : '*.jpeg; *.jpg; *.png; *.gif',      'buttonImage'   : 'uploadify/select.png',      'queueID'     : 'fileQueue',      'onUploadStart'  : function(file){        $('#uploadify').uploadify('settings', 'formData', {'iswatermark':$("#iswatermark").is(':checked')});  },      'onUploadSuccess' : function(file, data, response){        $(".fileWarp ul").append(SetImgContent(data));        SetUploadFile();      }    });});

点评:表单提交图片文件到服务器,成功后调用回调函数。

/*** @function imageUp*/public function imageUp(){      // 有文件传入,现在要做的就是把它保存起来    // 处理上传并返回数据    // 上传图片框中的描述表单名称,    $title = htmlspecialchars($_REQUEST['pictitle'], ENT_QUOTES);    // $path = htmlspecialchars($_REQUEST['dir'], ENT_QUOTES);    $savepath = htmlspecialchars($_REQUEST['savepath'], ENT_QUOTES);    $up = new think_upload();    $path = './uploadfile/'.$savepath;    //设置属性(上传的位置, 大小, 类型, 名是是否要随机生成)    $up -> set("path", $path);    $up -> set("maxsize", 2000000);    $up -> set("allowtype", array("gif", "png", "jpg","jpeg"));    $up -> set("israndname", true);    //使用对象中的upload方法, 就可以上传文件, 方法需要传一个上传表单的名子 pic, 如果成功返回true, 失败返回false    if($up -> upload("Filedata")) {      $name = $up->getFileName();      $return_data['url'] = $path.'/'.$name;      $return_data['title'] = $title;      $return_data['state'] = 'SUCCESS';      exit(json_encode($return_data));    } else {      $return_data['state'] = 'FAILURE';      $return_data['msg']  = $up->getErrorMsg();      exit(json_encode($return_data));    }}

点评:后台处理图片上传表单请求,返回图片路径

setOption($key, $val);   }   return $this;  }  /**   * 调用该方法上传文件   * @param string $fileFile 上传文件的表单名称    * @return bool    如果上传成功返回数true    */  function upload($fileField) {   $return = true;   /* 检查文件路径是滞合法 */   if( !$this->checkFilePath() ) {    $this->errorMess = $this->getError();    return false;   }   /* 将文件上传的信息取出赋给变量 */   $name = $_FILES[$fileField]['name'];   $tmp_name = $_FILES[$fileField]['tmp_name'];   $size = $_FILES[$fileField]['size'];   $error = $_FILES[$fileField]['error'];   /* 如果是多个文件上传则$file["name"]会是一个数组 */   if(is_Array($name)){      $errors=array();    /*多个文件上传则循环处理 , 这个循环只有检查上传文件的作用,并没有真正上传 */    for($i = 0; $i < count($name); $i++){      /*设置文件信息 */     if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i] )) {      if(!$this->checkFileSize() || !$this->checkFileType()){       $errors[] = $this->getError();       $return=false;       }     }else{      $errors[] = $this->getError();      $return=false;     }     /* 如果有问题,则重新初使化属性 */     if(!$return)           $this->setFiles();    }    if($return){     /* 存放所有上传后文件名的变量数组 */     $fileNames = array();        /* 如果上传的多个文件都是合法的,则通过销魂循环向服务器上传文件 */     for($i = 0; $i < count($name); $i++){       if($this->setFiles($name[$i], $tmp_name[$i], $size[$i], $error[$i] )) {       $this->setNewFileName();        if(!$this->copyFile()){        $errors[] = $this->getError();        $return = false;       }       $fileNames[] = $this->newFileName;       }          }     $this->newFileName = $fileNames;    }    $this->errorMess = $errors;    return $return;   /*上传单个文件处理方法*/   } else {    /* 设置文件信息 */    if($this->setFiles($name,$tmp_name,$size,$error)) {     /* 上传之前先检查一下大小和类型 */     if($this->checkFileSize() && $this->checkFileType()){       /* 为上传文件设置新文件名 */      $this->setNewFileName();       /* 上传文件 返回0为成功, 小于0都为错误 */      if($this->copyFile()){        return true;      }else{       $return=false;      }     }else{      $return=false;     }    } else {     $return=false;     }    //如果$return为false, 则出错,将错误信息保存在属性errorMess中    if(!$return)     $this->errorMess=$this->getError();     return $return;   }  }  /**    * 获取上传后的文件名称   * @param void  没有参数   * @return string 上传后,新文件的名称, 如果是多文件上传返回数组   */  public function getFileName(){   return $this->newFileName;  }  /**   * 上传失败后,调用该方法则返回,上传出错信息   * @param void  没有参数   * @return string 返回上传文件出错的信息报告,如果是多文件上传返回数组   */  public function getErrorMsg(){   return $this->errorMess;  }  /* 设置上传出错信息 */  private function getError() {   $str = "上传文件{$this->originName}时出错 : ";   switch ($this->errorNum) {    case 4: $str .= "没有文件被上传"; break;    case 3: $str .= "文件只有部分被上传"; break;    case 2: $str .= "上传文件的大小超过了HTML表单中MAX_FILE_SIZE选项指定的值"; break;    case 1: $str .= "上传的文件超过了php.ini中upload_max_filesize选项限制的值"; break;    case -1: $str .= "未允许类型"; break;    case -2: $str .= "文件过大,上传的文件不能超过{$this->maxsize}个字节"; break;    case -3: $str .= "上传失败"; break;    case -4: $str .= "建立存放上传文件目录失败,请重新指定上传目录"; break;    case -5: $str .= "必须指定上传文件的路径"; break;    default: $str .= "未知错误";   }   return $str.'
'; } /* 设置和$_FILES有关的内容 */ private function setFiles($name="", $tmp_name="", $size=0, $error=0) { $this->setOption('errorNum', $error); if($error) return false; $this->setOption('originName', $name); $this->setOption('tmpFileName',$tmp_name); $aryStr = explode(".", $name); $this->setOption('fileType', strtolower($aryStr[count($aryStr)-1])); $this->setOption('fileSize', $size); return true; } /* 为单个成员属性设置值 */ private function setOption($key, $val) { $this->$key = $val; } /* 设置上传后的文件名称 */ private function setNewFileName() { if ($this->israndname) { $this->setOption('newFileName', $this->proRandName()); } else{ $this->setOption('newFileName', $this->originName); } } /* 检查上传的文件是否是合法的类型 */ private function checkFileType() { if (in_array(strtolower($this->fileType), $this->allowtype)) { return true; }else { $this->setOption('errorNum', -1); return false; } } /* 检查上传的文件是否是允许的大小 */ private function checkFileSize() { if ($this->fileSize > $this->maxsize) { $this->setOption('errorNum', -2); return false; }else{ return true; } } /* 检查是否有存放上传文件的目录 */ private function checkFilePath() { if(empty($this->path)){ $this->setOption('errorNum', -5); return false; } if (!file_exists($this->path) || !is_writable($this->path)) { if (!@mkdir($this->path, 0755)) { $this->setOption('errorNum', -4); return false; } } return true; } /* 设置随机文件名 */ private function proRandName() { $fileName = date('YmdHis')."_".rand(100,999); return $fileName.'.'.$this->fileType; } /* 复制上传文件到指定的位置 */ private function copyFile() { if(!$this->errorNum) { $path = rtrim($this->path, '/').'/'; $path .= $this->newFileName; if (@move_uploaded_file($this->tmpFileName, $path)) { return true; }else{ $this->setOption('errorNum', -3); return false; } } else { return false; } }}

点评:php文件上传类。

然后处理好,图片数据删除对应的文件上传,减轻服务器空间压力。

public function delete() {    $id = intval($_GET['id']);    $this->db->delete(array('id'=>$id));    // 遍历删除原图片    $product_img = $this->product_img_db->select(array('product_id'=>$id));    foreach ($product_img as $k => $v) {      unlink($v['img']);    }    // 删除表数据    $this->product_img_db->delete(array('product_id'=>$id));    exit("1");}

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《php文件操作总结》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

  • PHP Yaf框架的简单安装使用教程(推荐)
  • PHP扩展框架之Yaf框架的安装与使用
  • yii,CI,yaf框架+smarty模板使用方法
  • jQuery.Uploadify插件实现带进度条的批量上传功能
  • BootStrap中Datetimepicker和uploadify插件应用实例小结
  • firefox浏览器用jquery.uploadify插件上传时报HTTP 302错误
  • jquery.uploadify插件在chrome浏览器频繁崩溃解决方法
  • 基于yaf框架和uploadify插件,做的一个导入excel文件,查看并保存数据的功能


  • 上一条:
    PHP实现的同步推荐操作API接口案例分析
    下一条:
    PHP获取指定日期是星期几的实现方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(95个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客