thinkphp5+layui上传文件
js页面中代码
layui.use('upload',function(){
var upload =layui.upload,
jq =layui.jquery;
upload.render({
elem:'.test'
,url:'{:url("home/jie/upload")}'
,accept:'file'
,area: ['500', '500px']
,before:function(input){
loading = layer.load(2, {
shade: [0.2,'#000']
});
}
,done:function(res){
layer.close(loading);
jq('input[name=img]').val(res.id);
// img.src = ""+res.message;
layer.msg(res.msg, {icon:1, time:1000});
}
});
})
thinkphp中的代码注意其中DAMAIN为index.php中定义的xpyclub中的域名
public function upload()
{
// 获取表单上传文件
$file = request()->file('file');
// 移动到框架应用根目录/public/uploads/ 目录下
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
if ($info) {
$data = [
// 引号问题 可以服务器有问题
'path' => 'uploads/' . str_replace('\\', '/', $info->getSaveName()),
'create_time' => time(),
'size' => $info->getSize(),
];
// Db::name( 'works' )->insert( $data );
$id = Db::name('works')->insertGetId($data);
return json_encode(['message' => DAMAIN . 'uploads/' . $info->getSaveName(), 'state' => 1, 'id' => $id], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
} else {
// 上传失败获取错误信息
return json_encode(['state' => 0, 'message' => $file->getError()]);
}
}