如何使用 php laravel 显示上传的图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44559142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display uploaded image with php laravel
提问by Khant Zin
How can I display uploaded image from stored location and I want to carry over the same image name to
如何显示从存储位置上传的图像,我想将相同的图像名称结转到
<input id = "Picture" type="file" name="Picture" accept="image/*" />
This code is used in Controller.php
when upload
Controller.php
上传时使用此代码
$img = $input['Picture'];
$name = $img->getClientOriginalName();
$uploaded = $img->move('public/img', $name);
This is where uploaded image will show in my new.blade.php
这是上传的图像将显示在我的 new.blade.php
<img src="#" id="Image" name="Image" />
And Image name doesn't save original name to mysql, it saved like C:xampp mpphpB7CF.tmp
并且图像名称不会将原始名称保存到 mysql,它保存为 C:xampp mpphpB7CF.tmp
How can I do to save original name that I Uploaded.
我该如何保存我上传的原始名称。
回答by Pankaj Makwana
Check below code this is working code.
检查下面的代码这是工作代码。
$file = $request->file('Picture');
$destinationPath = 'public/img/';
$originalFile = $file->getClientOriginalName();
$file->move($destinationPath, $originalFile);
This line $originalFile = $file->getClientOriginalName();
will assign original file name to $originalFile
and use this variable name where you insert image in database.
此行将$originalFile = $file->getClientOriginalName();
分配原始文件名 $originalFile
并在您在数据库中插入图像的位置使用此变量名。
Add below line to display image you have uploaded.
添加以下行以显示您上传的图像。
<img src='{{ asset('img/'.$originalFile) }}'>
<img src='{{ asset('img/'.$originalFile) }}'>
回答by thep nguyen nhu
$path = '';
if ($request->hasFile('Picture')) {
$file = $request->file('Picture');
$path = $file->storeAs(public_path('img'), $imageName);
//or
if (file_put_contents(public_path(img) . $imageName, $file)) {
$path = public_path(img) . $imageName;
}
}
return view('your-view', ['img_path' => $path]);;
and in your new.blade.php
并且在你的 new.blade.php
<img src="{{ $img_path }}" id="Image" name="Image" />
回答by shanthan
In Controller
在控制器中
public function show($name)
{
$extension = File::extension($name);
$path = public_path('storage/' . $name);
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;
}
}
Route
路线
Route::get('/images/{name}', 'FileController@show');
Index.blade.php
索引.blade.php
<a href='{{ asset("images/$hire_details->file") }}'>click here to view image</a>
回答by AddWeb Solution Pvt Ltd
I think you can try this:
我想你可以试试这个:
use Input;
use Image;
$file = Input::file('Picture');
$imageName = $file->getClientOriginalName();
$imgUpload = Image::make($file)->save(public_path('img/' . $imageName));
<img src="{{ asset('img/'.$imageName) }}">
Note: If you have get error for Image
then install package for Image
from this link
注意:如果您收到错误,Image
请Image
从此链接安装软件包
回答by Rahul
Return your $uploaded from controller to view
返回您从控制器上传的 $ 以查看
return view('new',['uploaded'=>$uploaded]);
and then in your view
然后在你看来
<img src='{{ asset($uploaded) }}' >
回答by Govind Samrow
Return path from controller and display :
从控制器和显示器返回路径:
<img src='{{ asset('img/'.$name) }}'>