[英]Call to a member function getClientOriginalExtension() on string in Laravel
[英]how to fix Call to a member function getClientOriginalExtension() on null in laravel 5.6
在我的laravel應用程序中,我以編程方式使用dropzone上傳圖像。 這是我的控制器,用於將圖像存儲在VehicleController中
public function store(Request $request)
{
$photos = $request->file('file');
if (!is_array($photos)) {
$photos = [$photos];
}
if (!is_dir($this->photos_path)) {
mkdir($this->photos_path, 0777);
}
for ($i = 0; $i < count($photos); $i++) {
$photo = $photos[$i];
$name = sha1(date('YmdHis') . str_random(30));
$save_name = $name . '.' . $photo->getClientOriginalExtension();//this is line 75
$resize_name = $name . str_random(2) . '.' . $photo->getClientOriginalExtension();
Image::make($photo)
->resize(250, null, function ($constraints) {
$constraints->aspectRatio();
})
->save($this->photos_path . '/' . $resize_name);
$photo->move($this->photos_path, $save_name);
$upload = new Upload();
$upload->filename = $save_name;
$upload->resized_name = $resize_name;
$upload->original_name = basename($photo->getClientOriginalName());
$upload->save();
}
return Response::json([
'message' => 'Image saved Successfully'
], 200);
但是當我的表單提交按鈕單擊后,發生以下錯誤,
1/1) FatalErrorException
Call to a member function getClientOriginalExtension() on null
in VehicleController.php line 75
如何解決這個問題?
我認為您的循環不合適。
嘗試下面的代碼。
$photos = $request->file('file');
foreach($photos as $photo){
$name = sha1(date('YmdHis') . str_random(30));
$save_name = $name . '.' . $photo->getClientOriginalExtension();
$resize_name = $name . str_random(2) . '.' . $photo->getClientOriginalExtension();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.