上传图片
public function image()
{
$obFile = request()->file('image');
if (!$obFile) {
return $this->error('请上传文件');
}
$sOldName = $obFile->getClientOriginalName();//原名
$sFileType = $obFile->getClientMimeType(); //文件类型
$sExtensionName = $obFile->getClientOriginalExtension();//扩展名
$sPath = $obFile->getRealPath(); //文件存储的位置路径
$arrAllowExtension = ['jpg', 'png', 'jpeg', 'gif', 'bmp'];
if (!in_array($sExtensionName, $arrAllowExtension)) {
return $this->error('必须是 jpeg, bmp, png, gif chan');
}
//组装文件存储的位置和自定义文件名
$path = "upload/goods/" . date("Ymd");
if (!is_dir($path)) { //判断目录是否存在 不存在就创建
mkdir($path, 0777, true);
chmod($path, 0777);
}
$imageName = "p_" . date("His", time()) . "_" . rand(1111, 9999);
$sFileName = 'goods/' . date('Ymd') . '/' . $imageName . '.' . $sExtensionName;
//调用laravel系统封装好的函数把图片存储到指定文件下
$bRet = Storage::disk('admin')->put($sFileName, file_get_contents($sPath));
if (!$bRet) {
return $this->error('上传失败');
}
return $this->success('ok', $this->domain() . '/upload/' . $sFileName);
}