【问题标题】:Laravel upload file form not workingLaravel上传文件表单不起作用
【发布时间】:2016-07-28 03:46:11
【问题描述】:

我的文件上传似乎无法正常工作。文件的函数 [例如 $file->getOriginalExtension();] 正在返回错误。

这是我的看法:

 <form class="form-horizontal" method="POST" action="/project/file_post.php" enctype="multipart/form-data">

    <---other text inputs--->
    Photo: <input type="file" name="photo"  />

    <input type="submit" class="btn btn-success btn-inline" value="SAVE ">

</form>

控制器:

//code below returns only the filename
$input = Input::all();
$file = array_get($input,'photo');
print_r($file);

//but when using this..it returns an error
$extension = $file->getClientOriginalExtension(); 

DD($input) 返回:

array:8 [▼
  "_token" => "6eScEZe1SLL72JDrQjmBJllNyiHaT8hdGKKMtjsD"
  "photo" => "test_test_2016.jpg"
  "field2" => "test"
  "field3" => "test"
  "field4" => "test"
  "field5" => "test"
  "field6" => "test"
  "field7" => "test"
]

我做错了吗?它返回文件的文件名,但是在使用函数时,它会返回各种错误(例如,在字符串上调用成员函数 getClientOriginalExtension())

你能帮忙吗?谢谢!

附录:

$photo = $request->file('photo');
echo $photo;

上面的代码也不起作用。没有错误。只返回空白。

【问题讨论】:

  • 添加表单action="post_page.php"; 我认为您的代码中缺少它。修改提交表单后代码运行的页面地址。
  • @JeesKDenny,我忘了把它放在我的帖子中,但我的实际代码中并没有丢失它。
  • 不能这样直接使用吗? $name = Input::file('photo')->getClientOriginalName();

标签: php file upload laravel-5.2


【解决方案1】:

根据你的 laravel 版本,你需要使用 Input::file 所以在你的控制器中使用类似的东西

if (Input::hasFile('photo')) {
    $photoFile = Input::file('photo');
    $extension = $photoFile->getClientOriginalExtension(); 
}

【讨论】:

    【解决方案2】:

    请求$request 为我工作。

    if($request->hasFile('photo')) {
       $photoExt = $request->file('photo')->getClientOriginalExtension();
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-24
      • 2011-09-05
      • 2021-02-03
      • 2017-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      相关资源
      最近更新 更多