function uploadAvatar(Request $request)
{
$user_id = Auth::id();
$avatar = $request->file('avatar')->store('/public/' . date('Y-m-d') . '/avatars');
//上傳的頭像字段avatar是文件類型
$avatar = Storage::url($avatar);//就是很簡單的一個步驟
$resource = Resource::create(['type' => 1, 'resource' => $avatar, 'user_id' => $user_id]);
if ($resource) {
return $this->responseForJson(ERR_OK, 'upload success');
}
return $this->responseForJson(ERR_EDIT, 'upload fails');
}
function upload_img($file)
{
$url_path = 'uploads/cover';
$rule = ['jpg', 'png', 'gif'];
if ($file->isValid()) {
$clientName = $file->getClientOriginalName();
$tmpName = $file->getFileName();
$realPath = $file->getRealPath();
$entension = $file->getClientOriginalExtension();
if (!in_array($entension, $rule)) {
return '圖片格式為jpg,png,gif';
}
$newName = md5(date("Y-m-d H:i:s") . $clientName) . "." . $entension;
$path = $file->move($url_path, $newName);
$namePath = $url_path . '/' . $newName;
return $path;
}
}
以上這篇laravel實現(xiàn)上傳圖片的兩種方式小結(jié)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。