【问题标题】:How to display uploaded image with php laravel如何使用 php laravel 显示上传的图片
【发布时间】:2017-11-17 10:53:52
【问题描述】:

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

<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

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

【问题讨论】:

    标签: php html laravel phpmyadmin


    【解决方案1】:

    检查下面的代码,这是工作代码。

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

    $originalFile = $file-&gt;getClientOriginalName(); 这一行会将原始文件名分配给$originalFile,并在您将图像插入数据库的位置使用此变量名。

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

    &lt;img src='{{ asset('img/'.$originalFile) }}'&gt;

    【讨论】:

    • 对不起!!保存的名称仍然不是原始名称。我使用了 phpMyAdmin
    • 这是我的工作代码,请检查; ` $file = $request->file('图片'); $destinationPath = 'public/img/'; $originalFile = $file->getClientOriginalName(); $file->move($destinationPath, $originalFile); $productImage = \App\ProductImage::find(4); $productImage->product_image = $originalFile; $productImage->update();`
    • 上传到公用文件夹的图片如何上传?
    【解决方案2】:
    $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" />
    

    【讨论】:

      【解决方案3】:

      在控制器中

      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>
      

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

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

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

          然后在你的视野中

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

          【讨论】:

          • 谢谢!!但是我的 new.blade 中有一个 标签。我想在里面显示。
          • 从控制器返回视图,然后在其中显示
          【解决方案6】:

          我想你可以试试这个:

          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 的软件包

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-12-04
            • 2011-10-17
            • 1970-01-01
            • 1970-01-01
            • 2021-05-09
            • 2020-12-19
            • 2014-07-25
            相关资源
            最近更新 更多