如何使用php laravel显示上传的图像

时间:2017-06-15 05:22:06

标签: php html laravel phpmyadmin

如何从存储的位置显示上传的图像,我想将相同的图像名称转移到

<input id = "Picture" type="file" name="Picture" accept="image/*" />

此代码在上传时使用Controller.php

$img = $input['Picture'];
$name = $img->getClientOriginalName();
$uploaded = $img->move('public/img', $name);

这是我的new.blade.php

中显示的已上传图片的位置
<img src="#" id="Image" name="Image" />

并且图片名称不会将原始名称保存到mysql,它保存为C:xampp mpphpB7CF.tmp

如何保存我上传的原始名称。

6 个答案:

答案 0 :(得分:1)

检查以下代码,这是正常工作的代码。

$file = $request->file('Picture');
$destinationPath = 'public/img/';
$originalFile = $file->getClientOriginalName();
$file->move($destinationPath, $originalFile);

此行$originalFile = $file->getClientOriginalName();会将原始文件名分配给$originalFile,并使用此变量名称在数据库中插入图像。

添加以下行以显示您上传的图片。

<img src='{{ asset('img/'.$originalFile) }}'>

答案 1 :(得分:1)

$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]);;

并在new.blade.php

<img src="{{ $img_path }}" id="Image" name="Image" />

答案 2 :(得分:0)

返回从控制器上传的$以查看

return view('new',['uploaded'=>$uploaded]);

然后在你的视图中

<img src='{{ asset($uploaded) }}' >

答案 3 :(得分:0)

从控制器和显示器返回路径:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     version="3.0">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <module-name>Admin Application</module-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servletContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

答案 4 :(得分:0)

我想你可以试试这个:

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) }}">

注意:如果Image收到错误,请从此link

安装Image的包

答案 5 :(得分:0)

在控制器

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::get('/images/{name}', 'FileController@show');

Index.blade.php

<a href='{{ asset("images/$hire_details->file") }}'>click here to view image</a>