Laravel 上传文件表单无效

Laravel upload file form not working

我的文件上传好像没用。文件的函数 [例如 $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($输入)returns:

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

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

你能帮忙吗?谢谢!

附录:

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

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

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

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

请求 $request 对我有用。

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