这篇文章主要介绍了关于thinkPHP5框架整合plupload实现图片批量上传,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

本文实例讲述了thinkPHP5框架整合plupload实现图片批量上传功能的方法。分享给大家供大家参考,具体如下:

在官网下载plupload http://http//www.plupload.com

或者点击此处本站下载。

这里我们使用的是pluploadQueue

在HTML页面引入相应的css和js,然后根据示例代码修改为自己的代码

{:lang('photo')}

{:lang('plupupload_tip')}

$(function() {

// Setup html5 version

$("#uploader").pluploadQueue({

// General settings

runtimes : 'html5,flash,silverlight,html4',

url : '{:url("photo/upphoto")}',

chunk_size: '1mb',

rename : true,

dragdrop: true,

filters : {

// Maximum file size

max_file_size : '10mb',

// Specify what files to browse for

mime_types: [

{title : "Image files", extensions : "jpg,gif,png"}

]

},

// Resize images on clientside if we can

resize : {width : 320, height : 240, quality : 90},

flash_swf_url : '/assets/plupupload/Moxie.swf',

silverlight_xap_url : '/assets/plupupload/Moxie.xap',

init: {

PostInit: function() {

$('#uploaded').html("");

},

FileUploaded : function(uploader , files, result) {

up_image = result.response;

if(up_image != ""){

$("#uploaded").append(""); //这里获取到上传结果

}

}

}

});

});

plupload整合:

/*

* 文件上传

*

* Donald

* 2017-3-21

*/

namespace app\backend\logic;

use think\Model;

class Plupupload extends Model{

public function upload_pic($file_type="data"){

#!! IMPORTANT:

#!! this file is just an example, it doesn't incorporate any security checks and

#!! is not recommended to be used in production environment as it is. Be sure to

#!! revise it and customize to your needs.

// Make sure file is not cached (as it happens for example on iOS devices)

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

header("Cache-Control: no-store, no-cache, must-revalidate");

header("Cache-Control: post-check=0, pre-check=0", false);

header("Pragma: no-cache");

/*

// Support CORS

header("Access-Control-Allow-Origin: *");

// other CORS headers if any...

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

exit; // finish preflight CORS requests here

}

*/

// 5 minutes execution time

@set_time_limit(5 * 60);

// Uncomment this one to fake upload time

// usleep(5000);

// Settings

//重新设置上传路径

$uploads = config('uploads_dir');

if(!empty($file_type)){

$uploads = $uploads .$file_type."/".date("Ymd");

}

$targetDir = $uploads;

//$targetDir = 'uploads';

$cleanupTargetDir = true; // Remove old files

$maxFileAge = 5 * 3600; // Temp file age in seconds

// Create target dir

if (!file_exists($targetDir)) {

@mkdir($targetDir);

}

// Get a file name

if (isset($_REQUEST["name"])) {

$fileName = $_REQUEST["name"];

} elseif (!empty($_FILES)) {

$fileName = $_FILES["file"]["name"];

} else {

$fileName = uniqid("file_");

}

//重命名文件

$fileName_arr = explode(".", $fileName);

$fileName = myrule().".".$fileName_arr[1]; //rule()请查看上篇我的上篇博客thinkphp同时上传多张图片文件重名问题

$filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;

// Chunking might be enabled

$chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;

$chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;

// Remove old temp files

if ($cleanupTargetDir) {

if (!is_dir($targetDir) || !$dir = opendir($targetDir)) {

die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');

}

while (($file = readdir($dir)) !== false) {

$tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;

// If temp file is current file proceed to the next

if ($tmpfilePath == "{$filePath}.part") {

continue;

}

// Remove temp file if it is older than the max age and is not the current file

if (preg_match('/\.part$/', $file) && (filemtime($tmpfilePath) < time() - $maxFileAge)) {

@unlink($tmpfilePath);

}

}

closedir($dir);

}

// Open temp file

if (!$out = @fopen("{$filePath}.part", $chunks ? "ab" : "wb")) {

die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');

}

if (!empty($_FILES)) {

if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {

die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');

}

// Read binary input stream and append it to temp file

if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) {

die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');

}

} else {

if (!$in = @fopen("php://input", "rb")) {

die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');

}

}

while ($buff = fread($in, 4096)) {

fwrite($out, $buff);

}

@fclose($out);

@fclose($in);

// Check if file has been uploaded

if (!$chunks || $chunk == $chunks - 1) {

// Strip the temp .part suffix off

rename("{$filePath}.part", $filePath);

}

// Return Success JSON-RPC response

die($filePath); //这里直接返回结果

// die('{"jsonrpc" : "2.0", "result" : "'.$filePath.'", "id" : "id"}');

}

}

最后Controller或Model获取结果并保存

$images = $request->post('images/a'); //这里一定要注意, thinkphp通过name获取post数组时会获取不到数据,需要在name后加/a,表示获取数组详见Request的typeCast

model('PhotoImage')->query_insert($images, $id);//批量插入图片

/**

* 强制类型转换

* @param string $data

* @param string $type

* @return mixed

*/

private function typeCast(&$data, $type)

{

switch (strtolower($type)) {

// 数组

case 'a':

$data = (array) $data;

break;

// 数字

case 'd':

$data = (int) $data;

break;

// 浮点

case 'f':

$data = (float) $data;

break;

// 布尔

case 'b':

$data = (boolean) $data;

break;

// 字符串

case 's':

default:

if (is_scalar($data)) {

$data = (string) $data;

} else {

throw new \InvalidArgumentException('variable type error:' . gettype($data));

}

}

}

相关推荐:

plupload php实例,thinkPHP5框架整合plupload实现图片批量上传相关推荐

  1. SSM框架:springmvc实现图片的上传与图片上传路径的设置

    说明:这个图片类文件上传的步骤是我经过验证的,在SSM框架下完成,搭建框架的部分不在这里说明. 第一步:添加两个项目需要的依赖.(pom.xml) <dependency><grou ...

  2. php plupload,thinkphp5 框架结合plupload实现图片批量上传功能示例

    本文实例讲述了thinkphp5 框架结合plupload实现图片批量上传功能.分享给大家供大家参考,具体如下: 在extend目录下新增目录uploader,并新建类Uploads namespac ...

  3. struts2框架单文件、多文件上传实例详解

    版权声明:本文为博主原创文章,如需转载,请标明出处. https://blog.csdn.net/alan_liuyue/article/details/79390681 简介 1.上一篇博客讲解了J ...

  4. WEB版一次选择多个文件进行批量上传(Plupload)的解决方案

    说明:Plupload支持多种浏览器,多种上传方式! 一般的WEB方式文件上传只能使用FileUpload控件进行一个文件一个文件的进行上传,就算是批量上传,也要把文件一个一个的添加到页面,无法如 w ...

  5. 基于Plupload的图片压缩上传

    前言 这里的上传工具基于JQuery.Plupload 传送门:Plupload官方.中文文档 为什么要做图片压缩? 现在手机拍照都快10M了,但是有时候图片上传只要看得清楚就可以了,比如上传身份证2 ...

  6. layui实现文件压缩上传_基于SSM框架、Layui的多文件上传、包括图片,压缩包,音频等文件(与数据库挂钩) - 爱秧博客...

    写在前面:当初为了实现一个多文件上传可是费了一番功夫,经过我日日夜夜的百度咨询,写了好几种方法,最终还是没能解决问题.我可以很负责任的告诉你,你去百度上不管你形容有多好,只要是涉及多文件,就会查到Mu ...

  7. SpringBoot(24) 整合七牛云实现文件上传

    一.前言 本文将基于springboot2.1.8.RELEASE整合七牛云实现文件上传 本文参考 https://www.keppel.fun/articles/2019/02/27/1551262 ...

  8. SpringBoot整合oss实现文件的上传,查看,删除,下载

    springboot整合oss实现文件的上传,查看,删除,下载 1.什么是对象存储 OSS? 答:阿里云对象存储服务(Object Storage Service,简称 OSS),是阿里云提供的海量. ...

  9. SpringBoot整合阿里云OSS文件上传、下载、查看、删除

    SpringBoot整合阿里云OSS文件上传.下载.查看.删除 该项目源码地址:https://github.com/ggb2312/springboot-integration-examples ( ...

最新文章

  1. 给力!斩获 GitHub 14000 Star,两周创办开源公司获数百万美元融资
  2. tms570 can 接收大量数据_CAN通讯系列--CAN总线基础3
  3. 北京工商大学计算机学院研究生院,北京工商大学计算机学院
  4. 《A Novel Pipeline Approach for Efficient Big Data Broadcasting》阅读报告
  5. [导入]一再的变故,终于决定何去何从.
  6. 2013网易实习生招聘笔试题
  7. [转载] Java标识符 数据类型 常量与变量
  8. Elasticsearch7.X 字段数据类型
  9. 什么时候会用到拷贝构造函数?
  10. fabric1.0 java sdk_运行 fabric-sdk-java 官方示例
  11. DBeaver执行SQL脚本文件
  12. 基于大数据的消费者洞察
  13. jcabanillas/yii2-inspinia-asset composert 安装失败
  14. python实现组合优化
  15. 2019.2.13 【过年停更了很久,挖个坑】
  16. 【第五章】 C语言之牛客网刷题笔记 【点进来保证让知识充实你一整天】
  17. Java中的GC简单介绍
  18. 深度学习 笔记(线性神经网络)
  19. 强烈推荐的音视频资料(文中有福利哈!)
  20. 数据库 SQLServer中GUID用法介绍

热门文章

  1. IOS OpenGL ES GPUImage 柔光混合 GPUImageSoftLightBlendFilter
  2. [前端]div不换行
  3. 做好项目管理,项目经理需要具备哪些优秀品质?
  4. threejs自定义顶点(vertex)创建几何体
  5. linux下安装php
  6. 银行网点计算机培训心得体会,银行营销培训心得体会
  7. ctf练习之就差一把钥匙
  8. python --人工智能专家系统---学生学习情况
  9. linux kodi 设置中文,Kodi 19 “Matrix” 开源家庭影院发布,界面设置成简体中文
  10. 什么是微信小程序基础库