Reputation: 969
I am using Laravel 5.0. I have a file upload function. It's going well. But I want to make the user only uploaded pdf files. I want when browse a file, the storage only show files with only .pdf files. And I want to have a size limit to file which want to upload.
Here is the view
<div class="form-group">
<label for="upload_file" class="control-label col-sm-2">Upload File</label>
<div class="col-sm-9">
<input class="form-control" type="file" name="upload_file" id="upload_file" required>
</div>
</div>
Here is the controller
$destination = 'files';
if($request->hasFile('upload_file')) {
$file = $request->file('upload_file');
$extension = $file->getClientOriginalExtension();
$file_name = str_replace('/','_',$request['nomor_surat']) . '-' . $kode_divisi[0]->kode_divisi . '.' . $extension;
$file->move($destination, $file_name );
}
$upload_file = $file_name;
$surat = new Surat();
$surat->upload_file = $upload_file;
$surat->save();
and what should I do next? what should I add in my controller and route and view?
Upvotes: 3
Views: 7150
Reputation: 5186
You can limit the user to browse PDF files only with the HTML file input accept
attribute:
<input type="file" accept="application/pdf" />
To limit the size, you can do it on the server side:
$file = $request->file('upload_file');
//no files larger than 700kb
if ($file->getSize() > 700000)
{
//respond not validated, file too big.
}
You can also validate the file type on the server as well:
if ($file->getClientMimeType() !== 'application/pdf')
{
//respond not validated, invalid file type.
}
Upvotes: 9