Dev*_*Dev 10 laravel laravel-5
我将所有用户文件上传到目录:
/resources/app/uploads/
Run Code Online (Sandbox Code Playgroud)
我尝试通过完整路径获取图像:
http://localhost/resources/app/uploads/e00bdaa62492a320b78b203e2980169c.jpg
Run Code Online (Sandbox Code Playgroud)
但我得到错误:
NotFoundHttpException in RouteCollection.php line 161:
Run Code Online (Sandbox Code Playgroud)
如何通过这条路径获得图像?
现在我尝试在根目录/ public/uploads /中的uplaod文件:
$destinationPath = public_path(sprintf("\\uploads\\%s\\", str_random(8)));
$uploaded = Storage::put($destinationPath. $fileName, file_get_contents($file->getRealPath()));
Run Code Online (Sandbox Code Playgroud)
它给了我错误:
Impossible to create the root directory
Run Code Online (Sandbox Code Playgroud)
Alp*_*sus 19
您可以专门制作用于显示图像的路线.
例如:
Route::get('/resources/app/uploads/{filename}', function($filename){
$path = resource_path() . '/app/uploads/' . $filename;
if(!File::exists($path)) {
return response()->json(['message' => 'Image not found.'], 404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
});
Run Code Online (Sandbox Code Playgroud)
所以现在你可以去localhost/resources/app/uploads/filename.png,它应该显示图像.
Ron*_*ald 14
您可以在刀片文件上尝试此操作.images文件夹位于公用文件夹中
<img src="{{URL::asset('/images/image_name.png')}}" />
Run Code Online (Sandbox Code Playgroud)
对于Laravel的更高版本(上面的5.7):
<img src = "{{ asset('/images/image_name.png') }}" />
Run Code Online (Sandbox Code Playgroud)
正如@alfonz 提到的,
resource_path()这是获取资源文件夹目录的正确方法。要获取特定文件位置,代码如下
$path = resource_path() . '/folder1/folder2/filename.extension';
Run Code Online (Sandbox Code Playgroud)
{{asset('path/to/your/image.jpg')}}如果您想从刀片中调用它,请尝试
要么
$url = asset('path/to/your/image.jpg'); 如果你想在你的控制器中.
希望它有帮助=)
| 归档时间: |
|
| 查看次数: |
37850 次 |
| 最近记录: |