js请求代码
layui.use('upload', function() {
var upload = layui.upload;
var loading;
//执行实例
var uploadInst = upload.render({
elem: '#export-excel1', //绑定元素
url: '{:url("shop.shop/daoruShop")}', //上传接口
before: function() {
loading = layer.load(1);
},
choose: function() {
layer.closeAll();
},
accept: 'file',
done: function(res) {
//上传完毕回调
// layer.msg('上传成功',{icon:1,time:1500});
if (res.code == '0') {
// $.msg.success('上传成功');
// $.tool.reload();
daoruShop(res.round,1,res.image)
} else {
$.msg.error(res.msg);
$.tool.reload();
}
},
error: function() {
//请求异常回调
layui.msg('上传失败', {
icon: 2,
time: 1500
});
}
});
});
function daoruShop(round,page,image) {
$.get('{:url("shop.Shop/daorufenpi")}', {
round: round,
page:page,
image:image
},
function(data) {
layer.msg(data.msg);
if (data.code == 1) {
if (data.allpage + 1 > data.page) {
setTimeout(function() {
daoruShop(data.round,data.page,data.image);
}, 1400)
} else {
layer.closeAll();
location.reload();
// window.location.href = '{:url("shop.Shop/daorufenpi")}?round='+data.round + '&page=' + data.page + '&image=' + data.image;
}
} else {
setTimeout(function() {
layer.msg(data.msg);
location.reload();
// layer.closeAll();
}, 1400)
}
}, "json");
}
PHP代码
public function daoruShop()
{
ini_set("memory_limit", "512M");
$file = request()->file('file');
if ($file) {
$file_types = explode(".", $_FILES['file']['name']); // ["name"] => string(25) "excel文件名.xls"
$file_type = $file_types[count($file_types) - 1]; //xls后缀
/*判别是不是.xls文件,判别是不是excel文件*/
if (strtolower($file_type) != "xls" && strtolower($file_type) != "xlsx" && strtolower($file_type) != "csv") {
$this->error('不是Excel文件,重新上传');
die;
}
$a = dirname($_SERVER['SCRIPT_FILENAME']) . "/";
$b = dirname(realpath($a)) . "/";
$path = $b . 'public/upload/excel';
$images = $b . 'public/static/uploads/shopImage';
$info = $file->move($path);
$file_path = $path . '/' . $info->getSaveName(); //上传后的EXCEL路径
// 引入核心文件
require '../vendor/phpoffice/phpexcel/Classes/PHPExcel/Reader/Excel2007.php';
require '../vendor/phpoffice/phpexcel/Classes/PHPExcel/Reader/Excel5.php';
if ($file_type == 'xlsx') {
$objReader = new \PHPExcel_Reader_Excel2007();
$objExcel = $objReader->load($file_path);
} else if