Laravel 5.2 上传图片到存储
Laravel 5.2 upload image to storage
我想问一下,如何让某人选择一张图片,然后他点击上传,图片就会保存到我的项目中(例如:project/public/images)。谢谢:)
你可以这样做:
$file = $request->file('image');
$name = $file->getClientOriginalName();
$path = public_path("images/$name");
Storage::put($path, File::get($file->getRealPath()));
不过。我建议使用 laravel 媒体库包:
https://docs.spatie.be/laravel-medialibrary/v4/introduction
这样你就可以做到:
$post->addMediaFromRequest('image')->toCollection('images');
否则你可以这样做
$destinationPath = '';
$filename = '';
if (Input::hasFile('file')) {
$file = Input::file('file');
$destinationPath = public_path().'/images/';
$filename = time() . '_' . $file->getClientOriginalName();
$filename = str_replace(' ','_',$filename);
$uploadSuccess = $file->move($destinationPath, $filename);
}
我想问一下,如何让某人选择一张图片,然后他点击上传,图片就会保存到我的项目中(例如:project/public/images)。谢谢:)
你可以这样做:
$file = $request->file('image');
$name = $file->getClientOriginalName();
$path = public_path("images/$name");
Storage::put($path, File::get($file->getRealPath()));
不过。我建议使用 laravel 媒体库包:
https://docs.spatie.be/laravel-medialibrary/v4/introduction
这样你就可以做到:
$post->addMediaFromRequest('image')->toCollection('images');
否则你可以这样做
$destinationPath = '';
$filename = '';
if (Input::hasFile('file')) {
$file = Input::file('file');
$destinationPath = public_path().'/images/';
$filename = time() . '_' . $file->getClientOriginalName();
$filename = str_replace(' ','_',$filename);
$uploadSuccess = $file->move($destinationPath, $filename);
}