TP6 + layui图片上传 本地上传好使,上传到服务器就不好使了,找两天没找到原因
代码如下,本地啥问题没有。
传服务器上就不行了
public function image() {
$file = request()->file('file');
try {
validate(['imgFile' => ['fileSize' => 999999999,'fileExt' => 'jpg,jpeg,png','fileMime' => 'image/jpg,image/jpeg,image/png']])->check(['imgFile' => $file]);
$saveName = \think\facade\Filesystem::disk('public')->putFile('cover', $file);
return JSON(array('status' => 0, 'msg' => "上传成功", 'path' => "upload/".$saveName));
} catch (\Exception $e) {
return json(array('status' => 1, 'msg' => "上传失败" . $e->getMessage()));
}
}
<button type="button" style="padding: 15px 60px;background-color: #fff;border: 1px #ccc solid;" id="coverupload" >
<i class="layui-icon layui-icon-upload"></i>选择图片
</button>
<input type="text" name="cover" style='display: none;'id="coverimg" readonly >
<img id='avatar' style='width: 130px; '>
layui.use(['form', 'jquery', 'layer', 'upload'], function () {
var form = layui.form,
layer = layui.layer,
upload = layui.upload;
upload.render({
elem: '#coverupload',
url: '{:url("upload/image")}',
accept: 'file',
size: 3 * 1024,
before: function (obj) {
obj.preview(function (index, file, result) {
console.log(file.name); //图片名字
console.log(file.type); //图片格式
console.log(result); //图片地址
});
},
done: function (res) {
console.log(res.path);
if (res.status === 0) {
layer.msg(res.msg, {
icon: 1
});
$('#avatar').attr('src', "/public/" + res.path);
$('#image').val("/public/" + res.path);
} else {
layer.msg(res.msg, {
icon: 2
});
}
}
});
});