Laravel商店上传了带文件名的图片路径

时间:2017-11-02 06:35:31

标签: php laravel-5

我想在db中存储上传的用户图像路径。这是我在控制器中的代码,

      $file = Input::file('pic');
      $img = Image::make($file);
      Response::make($img->encode('jpeg'));
      $filepath = $file->getPathName();

但上面的代码结果,  错误的路径,如 C:\ xampp \ tmp \ phpA0C8.tmp 我已经从我的laravel公共文件夹上传了图片。我尝试了很多代码,但他们并没有我怎么解决这个问题?有人可以帮忙吗?

3 个答案:

答案 0 :(得分:0)

您必须使用文件名定义路径。

    $FileName            = Input::file('pic')->getClientOriginalName();
    $FileDestinationPath = public_path('') .'/folder-name-in-public-folder/'.$FileName;

或者你可以使用商店方法获得它

$path = Input::file('pic')->store('folder-name');

return $path;

https://laravel.com/docs/5.5/filesystem

答案 1 :(得分:0)

使用商店方法

 $filepath = $request->file('pic')->store('pictures');

更多info

将代码放在回复之前

答案 2 :(得分:0)

使用laravel文件存储方法和hashname

  m = 0
  For Each htmlele1 In doc.getElementsByClassName("results")
  m = m + 1

  companyname = htmlele1.getElementsByTagName("h2")
  Address = htmlele1.getElementsByTagName("span")
   
  If Address.getAttribute("itemprop") = "Address" Then
 
   Cells(i, (m * 4 + 2)).Value = companyname.innerText + "," + Address.innerText
  End If
  
  Teliphone = htmlele1.getElementsByClassName("nolink")
  
  
  If Teliphone.getAttribute("itemprop") = "telephone" Then
  
  Cells(i, (m * 4 + 3)).Value = Teliphone.innerText

  End If
  

  no_of_property = htmlele1.getElementsByClassName("agents-stats-l")

  
If InStr(no_of_property.innerText, "Residential for sale") <> 0 Then
             Cells(i, (m * 4 + 4)).Value = Replace(no_of_property.innerText, "Residential for sale:", "")
                Else
                 Cells(i, (m * 4 + 4)).Value = 0
            End If
            Sale_price = htmlele1.getElementsByClassName("agents-stats-c")
             If InStr(Sale_price.innerText, "Avg. asking price") <> 0 Then
              Cells(i, (m * 4 + 5)).Value = Replace(Sale_price.innerText, "Avg. asking price: ", "")
              Else
                 Cells(i, (m * 4 + 5)).Value = 0
              End If

  Next

//配置/ filesystems.php

public function fileUPload(Request $request){
    $file = $request->file('pic');
    $dir = '/my_images/';
    $file->store($dir, 'public_path'); //store images
    echo $filePath= $dir . $file->hashName();
}