php图片上传 thinkphp5,thinkphp5图片上传接口
public function avatarUpload()
{
$file = request()->file(‘file‘);
$filePath = ‘a(chǎn)vatar‘;
$width = 200;
$height = 200;
if($file){
$filePaths = ROOT_PATH . ‘public‘ . DS . ‘uploads‘ . DS .$filePath;
if(!file_exists($filePaths)){
mkdir($filePaths,0777,true);
}
$info = $file->move($filePaths);
if($info){
$imgpath = $filePaths . ‘/‘ . $info->getSaveName();
$image = \think\Image::open($imgpath);
$image->thumb($width, $height)->save($imgpath);
$imgpath = ‘/uploads/‘.$filePath.‘/‘.$info->getSaveName();
$data = [
‘domain‘ => $_SERVER[‘HTTP_HOST‘],
‘filepath‘ => $imgpath,
];
return [‘code‘ => 0, ‘data‘ => $data, ‘msg‘ => ‘上傳成功‘];
}else{
// 上傳失敗獲取錯誤信息
return [‘code‘ => -1, ‘data‘ => ‘‘, ‘msg‘ => ‘上傳失敗‘];
}
}
}
function fileup(e){
var formData = new FormData();
formData.append(‘file‘, e.target.files[0]);
$.ajax({
url: ‘http://xxxxx.com/api/User/avatarUpload‘,
type: ‘POST‘,
cache: false,
data: formData,
processData: false,
contentType: false
}).done(function(res) {
console.log(res.data.domain);
console.log(res.data.filepath);
}).fail(function(res) {
});
}
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的php图片上传 thinkphp5,thinkphp5图片上传接口的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果获批新专利:机器学习追踪用户血液流动
- 下一篇: linux命令查询邮件发送状态,Linu