php 使用 Laravel 5 上传 pdf 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36715776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload pdf file using Laravel 5
提问by hendraspt
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:
我正在使用 Laravel 5.2,我想制作一个可以上传 pdf 文件的表单。我想将该文件添加到“public”文件夹中的“files”文件夹中。
这是我的观点:
<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?
我接下来该怎么做?我应该在我的控制器和路由中添加什么?
回答by Mustafa Ehsan Alokozay
First you should add enctype="multipart/form-data"
to your <form>
tag. Then in your controller handle the file upload as follow:
首先,您应该添加enctype="multipart/form-data"
到您的<form>
标签中。然后在您的控制器中按如下方式处理文件上传:
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.');
}
// ...
}
Link to Laravel Docs for Handling File Uploads
Laravel casts the file type params in request to UploadedFile
objects. You can see Symfony's UploadedFile
class herefor available methods and attributes.
Laravel 将请求中的文件类型参数转换为UploadedFile
对象。您可以在此处查看 Symfony 的UploadedFile
类以获取可用的方法和属性。
回答by Liam
First of all, the documentation tells you exactly what to do here.
首先,文档会准确地告诉您在这里做什么。
What you want to do is adding this to your <form>
tag:
enctype="multipart/form-data"
(This allows you to upload data), set a method
(get/post) and an action
(url).
您要做的是将其添加到您的<form>
标签中:(
enctype="multipart/form-data"
这允许您上传数据),设置一个method
(获取/发布)和一个action
(网址)。
Then you want to set up your routes.
然后你想设置你的路线。
For example:
Route::post('/pdf/upload', 'FileController@upload');
例如:
Route::post('/pdf/upload', 'FileController@upload');
This way you make sure that when you send the form it will go to your FileController
with upload
as function.
这样您就可以确保当您发送表单时,它会FileController
以upload
as 功能发送给您。
In your controller you want to declare the file as explained in the docs.$file = $request->file('photo');
.
在您的控制器中,您希望按照文档中的说明声明文件。$file = $request->file('photo');
.
From this point you can do whatever you'd like to do with the file ($file
). For example uploading it to your own server.
从这一点开始,您可以对文件 ( $file
)做任何您想做的事情。例如将其上传到您自己的服务器。
回答by Hamza Zymawy
回答by Sifb71
you can this code for upload file in Laravel:
您可以使用以下代码在 Laravel 中上传文件:
$request->file('upload_file')->move($path,$name);
回答by rashedcs
public function store(Request $request)
{
if($request->file('file'))
{
$file = $request->file('file');
$filename = time() . '.' . $request->file('file')->extension();
$filePath = public_path() . '/files/uploads/';
$file->move($filePath, $filename);
}
}
回答by Rubberduck1337106092
You can take a look at how i upload files, all files are accepted: first the code for the create.blade.php form
你可以看看我是如何上传文件的,所有文件都被接受:首先是create.blade.php表单的代码
{!! Form::open(
array(
'url' => 'uploads',
'method' => 'post',
'class' => 'form',
'novalidate' => 'novalidate',
'files' => true)) !!}
@include('uploadspanel.create_form')
{!! Form::close() !!}
Remember to set files to true
记住将文件设置为true
Then the uploadspanel.create_form
然后uploadspanel.create_form
<div class="form-group">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('file', 'Bestand:') !!}
{!! Form::file('file',null,['class'=>'form-control']) !!}
</div>
@if(\Auth::user()->level == 2)
<div class="form-group">
{{ Form::label('approved', 'Beschikbaar voor:') }}
{{ Form::select('approved', array(1 => 'Iedereen', 2 => 'monteurs', 3 => 'concept'), null, ['class' => 'form-control']) }}
</div>
@else
{{ Form::hidden('approved', 3) }}
@endif
<div class="form-group">
{!! Form::submit('Bestanden uploaden',['class' => 'btn btn-primary form-control']) !!}
</div>
then the controller store function
然后控制器存储功能
public function store(UploadRequest $request){
$extension = Input::file('file')->getClientOriginalExtension();
$filename = rand(11111111, 99999999). '.' . $extension;
Input::file('file')->move(
base_path().'/public/files/uploads/', $filename
);
if(\Auth::user()->level == 2) {
$approved = $request['approved'];
} else {
$approved = 3;
}
$fullPath = '/public/files/uploads/' . $filename;
$upload = new Uploads(array(
'name' => $request['name'],
'format' => $extension,
'path' => $fullPath,
'approved' => $approved,
));
$upload->save();
$uploads = Uploads::orderBy('approved')->get();
return view('uploadspanel.index', compact('uploads'));
}