Readytocode
  • Home
  • Laravel
  • PHP
  • React Js
  • Git
  • Server
Facebook X (Twitter) Instagram
Readytocode
  • Home
  • Laravel
  • PHP
  • React Js
  • Git
  • Server
Facebook X (Twitter) Instagram Pinterest LinkedIn Reddit
Readytocode
You are at:Home Laravel 9 Upload Zip and Extract on server

Laravel

Laravel 9 Upload Zip and Extract on server

JustinBy JustinJuly 25, 2022No Comments2 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Email




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">&times;</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..

 

 

Total
0
Shares
Share 0
Tweet 0
Pin it 0
Share 0

Like this:

Like Loading...

Related

file upload laravel zip extract Laravel zip upload upload zip
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Justin
  • Website

Related Posts

How to Generate image from text with Laravel 10

May 16, 2023

Laravel 10 Restrict User Access

May 3, 2023

Laravel 10 Pagination Example

May 3, 2023

Leave A Reply Cancel Reply

Featured Posts
  • Why meta tag is important for seo 1
    How to change react Page title, Page Description and some other meta Tag
    • August 4, 2023
  • 2
    How to Generate image from text with Laravel 10
    • May 16, 2023
  • Laravel 10 Restrict User Access 3
    Laravel 10 Restrict User Access
    • May 3, 2023
  • Laravel 10 Pagination Example 4
    Laravel 10 Pagination Example
    • May 3, 2023
  • install Steam on Ubuntu 5
    How to install Steam on Ubuntu 22.04 step by step
    • April 20, 2023



Readytocode
Facebook X (Twitter) Instagram Pinterest
  • About Us
  • Privacy Policy
© 2025 ReadyToCode.

Type above and press Enter to search. Press Esc to cancel.

%d