In this post, I will show you how we can upload a zip file on server and extract. Follow the steps below,
I hope you all already install Laravel , if not then use this link.
Step 1. Create a controller using artisan command.
php artisan make:controller ZipController
Step 2. Make route for View and Upload
Route::get("/adv-view-creative/{id}",[ZipController::class,"view_creative"]); Route::post("/adv-save-new-creative",[ZipController::class,"save_creative"]);
Step 3. Create View file
<div class="modal model-lg fade" id="myModal" role="dialog"> <div class="modal-dialog" style="max-width:800px !important;"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <h4 class="modal-title">New Creative</h4> <button type="button" class="close" onclick="hideModel();" data-dismiss="modal">×</button> </div> <form action="/adv-save-new-creative" method="post" enctype="multipart/form-data" style="background-color: white;"> <div class="modal-body"> <div class="row"> @csrf <div class="col-sm-12"> <div class="form-group"> <label for="exampleInputEmail3">Title</label> <input type="text" name="campaign_creative_title" class="form-control" id="campaign_target_url" placeholder="Title" value="@if(!empty(old('campaign_target_url'))){{old('campaign_target_url')}}@endif"/> </div> </div> <div class="col-sm-12"> <div class="form-group"> <label for="exampleInputEmail3">Descriptions</label> <textarea class="form-control" name="campaign_creative_desc"></textarea> </div> </div> <div class="col-sm-12"> <div class="form-group"> <label>HTML 5 banner Zip upload</label> <input type="file" name="file[]" multiple="multiple" class="form-control" accept=".zip" required> </div> </div> <div class="col-sm-6"> <div class="form-group"> <label>Click to Action</label> <select class="form-control form-control-sm" name="campaign_creative_ctr"> <option value="">Choose CTR</option> @if(!empty($ctr)) @foreach ($ctr as $v_ctr) <option value="{{$v_ctr->campaign_ctr_id}}">{{$v_ctr->campaign_ctr_name}}</option> @endforeach @endif </select> </div> </div> <div class="col-sm-6"> <div class="form-group"> <label>Target URL</label> <input type="text" name="campaign_creative_target_url" class="form-control" placeholder="Target URL"> </div> </div> </div> </div> <div class="modal-footer"> <button type="submit" class="btn btn-warning">Save</button> </div> </form> </div> </div> </div>
Step 4. Controller methods for view and upload.
First one for view
public function view_creative($id){ $data['info'] = "readytocode.net"; return view("creatives",$data); }
Now second method for upload
public function save_creative(Request $req){ if($req->file){ $files = $req->file('file'); $model=new AdvertiserCampaignCreativeModel(); $model->campaign_creative_title=$req->campaign_creative_title; $filename = time()."-".$file->getClientOriginalName(); $path = public_path().'/uploads/'; $file->move($path, $filename); $file_new_path='uploads/'. $filename; $model->campaign_creative_file = 'uploads/'. $filename; $file_extension=pathinfo('uploads/'. $filename, PATHINFO_EXTENSION); if($file_extension=="zip"){ $zip = new ZipArchive(); $zip_path = $zip->open($file_new_path); if ($zip_path === TRUE) { mkdir(str_replace(".","-",$file_new_path), 0777, true); $zip->extractTo(str_replace(".","-",$file_new_path)); $zip->close(); } }else{ return back()->with('error','Invalid ZIP Banner!'); } $model->save(); } return back()->with('success','New creative added.'); }
Note: At top of controller don’t forgot to include ZipArchive, like below code.
use ZipArchive;
Now run the code and test.
For Upload the zip file on server i uses
$filename = time()."-".$file->getClientOriginalName(); $path = public_path().'/uploads/'; $file->move($path, $filename);
For extracting i used
$zip = new ZipArchive(); $zip_path = $zip->open($file_new_path); if ($zip_path === TRUE) { mkdir(str_replace(".","-",$file_new_path), 0777, true); $zip->extractTo(str_replace(".","-",$file_new_path)); $zip->close(); }
Happy Coding..