I would like to allow the audio,doc and video file type in uploading file. My question is, how should I allow the audio,doc and video through file upload? In my code the file that allows are :png,gif,jpeg,txt,pdf,docx. This is my code
My controller
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Input;
use Validator;
use Redirect;
use Request;
use Session;
use App\Administrator;
class ApplyController extends Controller
{
public function multipleUpload()
{
return view('multiple_upload');
}
public function multipleUploadPost() {
// getting all of the post data
$files = Input::file('images');
// Making counting of uploaded images
$file_count = count($files);
// start count how many uploaded
$uploadcount = 0;
foreach($files as $file) {
$rules = array('file' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,docx'
$validator = Validator::make(array('file'=> $file), $rules);
if($validator->passes()){
$destinationPath = 'uploads';
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension() ;
if($extension == "jpg" || $extension == "PNG" )
{
$destinationPath = 'uploads/image';
}
else if ($extension == "docx" || $extension == "doc")
{
$destinationPath = 'uploads/documents';
}
else if ($extension == "pdf")
{
$destinationPath = 'uploads/pdf';
}
else if ($extension == "xls")
{
$destinationPath = 'uploads/excel';
}
$changeFileName = str_random(40).'.'.$extension;
$upload_success = $file->move($destinationPath, $changeFileName);
$uploadcount ++;
Administrator::create([ // Administrator is the name of my model
'original_filename' => $filename, // Insert the image to the database in column "image"
'change_filename' => $changeFileName // Insert the image to the database in column "image"
]);
}
}
if($uploadcount == $file_count){
Session::flash('success', 'Upload successfully');
return Redirect::to('multiple-upload');
}
else {
Session::flash('alert', 'Upload failed');
return Redirect::to('multiple-upload')->withInput()->withErrors($validator);
}
}
}
My view
<div class="text-content">
<div class="span7 offset1">
@if(Session::has('success'))
<div class="alert-box success">
<h2>{!! Session::get('success') !!}</h2>
</div>
@endif
<div class="secure">Upload form</div>
{!! Form::open(array('url'=>'apply/multiple_upload','method'=>'POST', 'files'=>true)) !!}
<div class="control-group">
<div class="controls">
{!! Form::file('images[]', array('multiple'=>true)) !!}
<p class="errors">{!!$errors->first('images')!!}</p>
@if(Session::has('error'))
<p class="errors">{!! Session::get('error') !!}</p>
@endif
</div>
</div>
{!! Form::submit('Submit', array('class'=>'send-btn')) !!}
{!! Form::close() !!}
</div>
use Mime validation which will allow only those format which you specify
for ex:
foreach ($files as $file){
$validator = Validator::make(
array(
'file' => $file,
'extension' => strtolower($file->getClientOriginalExtension()),
),
[
'file' => 'required|max:100000',
'extension' => 'required|in:mp3,mp4,doc',
],[
'file.required' => 'File is Required',
'extension.in' => ' File Should be of type mp3,mp4 or doc'
]
);
if($validator->fails()){
return response()->json(['status' => 'fail', 'data' => null, 'msg' => $validator->errors()], 200);
}
else{
dd("success");
}
}
List of mime types Available
http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
Hope this might help you