如何显示存储图像 Laravel 5.2
How to display an storage image Laravel 5.2
我想在我的视图中显示图像,但我收到:File not found
。
两个错误:
FileNotFoundException in FilesystemAdapter.php line 61:
storage/app/public/covers/teste-bravo_cover.jpg
FileNotFoundException in Filesystem.php line 386: File not found at
path: storage/app/public/covers/teste-bravo_cover.jpg
但图像在正确的文件夹中:
嗯,在 Laravel 5.2 storage:link
中行不通。
磁盘上的图像存储 - 可以很好地存储图像
if($cover){
dump("ok1");
Storage::disk('public_covers')->put($covername, File::get($cover));
$coverObject = New Cover();
//path com o nome do arquivo
$coverObject->imagePath = 'storage/app/public/covers/'.$covername;
dump($coverObject->imagePath);
}
//Salva a capa com o path para o arquivo
$coverObject->save();
我的文件系统(使用 "public_covers" 磁盘)- 可以很好地存储图像
'public_covers' => [
'driver' => 'local',
'root' => storage_path('app/public/covers'),
'visibility' => 'public',
],
查看代码:(如果电影有封面,请显示)
@if($movie->cover)
<img src="{{route('cover.image', [$movie->cover])}}" />
@endif
路由代码,指向控制器方法
Route::get('/movieimage/{coverId}',
[
'as' => 'cover.image',
'uses' => 'MovieController@getimage'
]
);
拍摄图像的控制器方法
public function getimage($coverId){
$movie = Cover::find($coverId);
//dump($movie);
$imagePath = $movie['imagePath'];
dump($imagePath);
$file = Storage::disk('public_covers')->get($imagePath);
$response = Response::make($file, 200);
return $response;
}
我是 laravel 的新手。我在 laravel 5.8 上遇到了同样的问题。我创建了 symlink 来将 pdf 显示为视图,但 FileNotFoundException
仍然存在。
我最终使用 url localhost:8000/storage/filename.ext
.
访问了 pdf
在 运行 ln -s /path/to/laravel/storage/public /path/to/laravel/public/storage
或 php artisan storage:link
之后,一个存储文件夹被添加到您的 appname/public
目录中。我认为一旦创建了 link,我们就可以通过创建的 public link 访问存储中的图像。尝试通过在 public
目录中创建的新 /appname/public/storage/app/pathtoimage
文件夹访问图像,而不是直接指向 /appname/storage
文件夹
<img src={{asset('/storage/pathtoimg')}} /> # using storage folder in public
我是如何解决问题的:
首先,将文件保存在 "public" 文件夹中。
在我的 filesystems.php 上,我更换了我的磁盘。现在我的根是 "public_path" 而不是 "storage_path"
'public_covers' => [
'driver' => 'local',
'root' => public_path('images/covers'),
'visibility' => 'public',
],
我的控制器方法
public function store(Request $request){
$cover = $request->file('cover');
$inputs = $this->request->all();
$this->validate($this->request, [
'title' => 'required|max:255',
'description' => 'required',
'gender' => 'required',
'secondGender' => 'required',
'year' => 'numeric|min:1900|max:2100',
'lenght' => 'numeric|min:10'
]);
//build movie model object
$movie = New Movie();
$movie->user_id = \Auth::user()->id;
$movie->gender_id = $inputs['gender'];
$movie->secondgender_id = $inputs['secondGender'];
$movie->title = $inputs['title'];
$movie->slug = str_slug($inputs['title']);
$movie->description = $inputs['description'];
$movie->lenght = $inputs['lenght'];
$movie->year = $inputs['year'];
//TODO Mudar a composição do nome da foto para impedir que no futuro se sobreponha uma capa com outro filme que venha a ter o mesmo nome
//TODO: Change the name createan to avoid overwrite the cover with an movie with the same name
$covername = str_slug($inputs['title']).'_cover' .'.jpg';
//if have an cover file
if($cover){
//storage the file on my public_covers (look my filesystems.php)
Storage::disk('public_covers')->put($covername, File::get($cover));
$coverObject = New Cover();
//path com o nome do arquivo
//path with the file name
$coverObject->imagePath = 'images/covers/'.$covername;
$coverObject->save();
$movie->cover_id = $coverObject->id;
}
$movie->save();
return redirect()
->route('user.dashboard')
->with([
'success' => 'Filme adicionado com sucesso!',
]);
}
我的查看图片代码
@if($movie->cover)
<img class="cover" src="{{ asset($movie->cover->imagePath) }}" alt="{{ $movie->title }}"">
@else
<img class="cover" src="{{ asset('images/noimage.jpg') }}" alt="{{ $movie->title }}">
@endif
你可以试试
需要包括这个
use Illuminate\Support\Facades\Storage;
并在控制器或路由中设置
Route::get('storage/{filename}', function ($filename)
{
$files = Storage::files('logs'); // set repo name of storage folder
$path = storage_path('public/' . $filename);
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
});
我想在我的视图中显示图像,但我收到:File not found
。
两个错误:
FileNotFoundException in FilesystemAdapter.php line 61: storage/app/public/covers/teste-bravo_cover.jpg
FileNotFoundException in Filesystem.php line 386: File not found at path: storage/app/public/covers/teste-bravo_cover.jpg
但图像在正确的文件夹中:
嗯,在 Laravel 5.2 storage:link
中行不通。
磁盘上的图像存储 - 可以很好地存储图像
if($cover){
dump("ok1");
Storage::disk('public_covers')->put($covername, File::get($cover));
$coverObject = New Cover();
//path com o nome do arquivo
$coverObject->imagePath = 'storage/app/public/covers/'.$covername;
dump($coverObject->imagePath);
}
//Salva a capa com o path para o arquivo
$coverObject->save();
我的文件系统(使用 "public_covers" 磁盘)- 可以很好地存储图像
'public_covers' => [
'driver' => 'local',
'root' => storage_path('app/public/covers'),
'visibility' => 'public',
],
查看代码:(如果电影有封面,请显示)
@if($movie->cover)
<img src="{{route('cover.image', [$movie->cover])}}" />
@endif
路由代码,指向控制器方法
Route::get('/movieimage/{coverId}',
[
'as' => 'cover.image',
'uses' => 'MovieController@getimage'
]
);
拍摄图像的控制器方法
public function getimage($coverId){
$movie = Cover::find($coverId);
//dump($movie);
$imagePath = $movie['imagePath'];
dump($imagePath);
$file = Storage::disk('public_covers')->get($imagePath);
$response = Response::make($file, 200);
return $response;
}
我是 laravel 的新手。我在 laravel 5.8 上遇到了同样的问题。我创建了 symlink 来将 pdf 显示为视图,但 FileNotFoundException
仍然存在。
我最终使用 url localhost:8000/storage/filename.ext
.
在 运行 ln -s /path/to/laravel/storage/public /path/to/laravel/public/storage
或 php artisan storage:link
之后,一个存储文件夹被添加到您的 appname/public
目录中。我认为一旦创建了 link,我们就可以通过创建的 public link 访问存储中的图像。尝试通过在 public
目录中创建的新 /appname/public/storage/app/pathtoimage
文件夹访问图像,而不是直接指向 /appname/storage
文件夹
<img src={{asset('/storage/pathtoimg')}} /> # using storage folder in public
我是如何解决问题的:
首先,将文件保存在 "public" 文件夹中。
在我的 filesystems.php 上,我更换了我的磁盘。现在我的根是 "public_path" 而不是 "storage_path"
'public_covers' => [
'driver' => 'local',
'root' => public_path('images/covers'),
'visibility' => 'public',
],
我的控制器方法
public function store(Request $request){
$cover = $request->file('cover');
$inputs = $this->request->all();
$this->validate($this->request, [
'title' => 'required|max:255',
'description' => 'required',
'gender' => 'required',
'secondGender' => 'required',
'year' => 'numeric|min:1900|max:2100',
'lenght' => 'numeric|min:10'
]);
//build movie model object
$movie = New Movie();
$movie->user_id = \Auth::user()->id;
$movie->gender_id = $inputs['gender'];
$movie->secondgender_id = $inputs['secondGender'];
$movie->title = $inputs['title'];
$movie->slug = str_slug($inputs['title']);
$movie->description = $inputs['description'];
$movie->lenght = $inputs['lenght'];
$movie->year = $inputs['year'];
//TODO Mudar a composição do nome da foto para impedir que no futuro se sobreponha uma capa com outro filme que venha a ter o mesmo nome
//TODO: Change the name createan to avoid overwrite the cover with an movie with the same name
$covername = str_slug($inputs['title']).'_cover' .'.jpg';
//if have an cover file
if($cover){
//storage the file on my public_covers (look my filesystems.php)
Storage::disk('public_covers')->put($covername, File::get($cover));
$coverObject = New Cover();
//path com o nome do arquivo
//path with the file name
$coverObject->imagePath = 'images/covers/'.$covername;
$coverObject->save();
$movie->cover_id = $coverObject->id;
}
$movie->save();
return redirect()
->route('user.dashboard')
->with([
'success' => 'Filme adicionado com sucesso!',
]);
}
我的查看图片代码
@if($movie->cover)
<img class="cover" src="{{ asset($movie->cover->imagePath) }}" alt="{{ $movie->title }}"">
@else
<img class="cover" src="{{ asset('images/noimage.jpg') }}" alt="{{ $movie->title }}">
@endif
你可以试试
需要包括这个
use Illuminate\Support\Facades\Storage;
并在控制器或路由中设置
Route::get('storage/{filename}', function ($filename)
{
$files = Storage::files('logs'); // set repo name of storage folder
$path = storage_path('public/' . $filename);
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
});