如何在laravel 5.1中创建上传文件?

时间:2015-12-14 01:59:15

标签: database laravel-5.1

我是Laravel 5.1

的新手

您能否帮助我了解如何上传docxPDFimage等文件,以便使用Laravel 5.1将其存储到数据库中?

我浏览了很多教程,但没有在Laravel 5.1我自己尝试但是它没有用。

  

中的NotFoundHttpException      C:\ Loucelle \ _ \   _ \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ RouteCollection.php第161行:

您可以通过提供任何示例代码来帮助我吗?

1 个答案:

答案 0 :(得分:2)

你的路线:

Route::post('uploadFile', 'YourController@uploadFile');

您的HTML刀片:

<form action="{{ url('uploadFile') }}" enctype="multipart/form-data">

    <input type="file" name="file">
    <input type="submit">

</form>

你的控制器:

public function uploadFile()
{
    //get the file
    $file = Input::file('file');

//create a file path
$path = 'uploads/';

//get the file name
$file_name = $file->getClientOriginalName();

//save the file to your path
$file->move($path , $file_name); //( the file path , Name of the file)

//save that to your database
$new_file = new Uploads(); //your database model
$new_file->file_path = $path . $file_name;
$new_file->save();

//return something (sorry, this is a  habbit of mine)
return 'something';
}

有用的资源(这些链接可能会过期,因此它们仅用于参考):

Laravel Requests (like Inputs and the such)

File Upload Tutorial that helped me when I started