使用Laravel 5上传pdf文件 [英] Upload pdf file using Laravel 5
问题描述
我正在使用Laravel 5.2,我想制作一个可以上传pdf文件的表格.我想将该文件添加到"public"文件夹中的"files"文件夹中.
这是我的观点:
I'm using Laravel 5.2 and I want to make a form which can upload a pdf file with it. I want to add that file on folder "files" in "public" folder.
here is my view:
<div class="form-group">
<label for="upload_file" class="control-label col-sm-3">Upload File</label>
<div class="col-sm-9">
<input class="form-control" type="file" name="upload_file" id="upload_file">
</div>
</div>
接下来我该怎么办?我应该在控制器和路由中添加什么?
and what should I do next? what should I add in my controller and route?
推荐答案
首先,您应该将enctype="multipart/form-data"
添加到<form>
标记中.然后在您的控制器中按以下方式处理文件上传:
First you should add enctype="multipart/form-data"
to your <form>
tag. Then in your controller handle the file upload as follow:
class FileController extends Controller
{
// ...
public function upload(Request $request)
{
$uniqueFileName = uniqid() . $request->get('upload_file')->getClientOriginalName() . '.' . $request->get('upload_file')->getClientOriginalExtension());
$request->get('upload_file')->move(public_path('files') . $uniqueFileName);
return redirect()->back()->with('success', 'File uploaded successfully.');
}
// ...
}
Laravel在请求中将文件类型参数转换为UploadedFile
对象.您可以在此处中看到Symfony的UploadedFile
类.方法和属性.
Laravel casts the file type params in request to UploadedFile
objects. You can see Symfony's UploadedFile
class here for available methods and attributes.
这篇关于使用Laravel 5上传pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!