Skip to content

Multiple File Import By Zip File

Derek Jones edited this page Jul 5, 2012 · 6 revisions

I got a bit sick of seeing multiform inputs in many different cms's where you have to select each file 1 at a time. To me this is completely useless. So I wrote a little function to import a zip file archive extract all the files, and import them into a specific folder (category named folder that's database driven) This is more designed for images with say a gallery in mind, but it can easily be adopted to be used with almost any file type. So this not only extracts the zip files but it also creates thumbnails, than moves all the files into the correct place, and then deletes all the source files and zip archive after it's done leaving no trace of the oringal filesm, so it doesn't waste space.

So first we have a the primary function.

function import_zip()
 {
 $this->form_validation->set_message('required', 'The Field %s is Required');
 $this->form_validation->set_rules('userfile', 'userfile', 'xss_clean');
 $this->form_validation->set_error_delimiters('<h5>', '</h5>');
 if($this->form_validation->run() == FALSE)
  {
  $data['error'] = '';
  $data['heading'] = 'Import Zip File';
  $data['page'] = 'import_zip';
  $data['vcate'] = $this->db->get('gallery_categories');
  $this->load->view('import_zip', $data);
  }
 else
  {
  //Upload file - Be Sure to change your upload path.
  $config['upload_path'] = './assets/gallery/photos/import_zip';
  $config['allowed_types'] = 'zip';
  $config['max_size'] = '20000';
  $this->load->library('upload', $config);
  if(!$this->upload->do_upload())
   {
   $data['error'] = array('error' => $this->upload->display_errors());
   $data['heading'] = 'Import Zip File';
   $data['vcate'] = $this->db->get('gallery_categories');
   $this->load->view('import_zip', $data);
   }
  else
   {
   #Get the Category Name
   $cat_name = $this->gallery_model->get_cat_name($this->input->post('cat_id'));
   foreach($cat_name as $ca)
    {
    $get_cat_name = $ca->name;
    }
   
   #Get the uploaded zip file
   $zip = new ZipArchive;
   $zip_array = array('upload_data' => $this->upload->data());
   $zip_file = $zip_array['upload_data']['full_path'];
   
   #Make a temp folder to store source zip files
   @mkdir($_SERVER['DOCUMENT_ROOT']. '/assets/gallery/photos/import_zip/' . url_title($get_cat_name));
   
   #Open the Zip File, and extract it's contents.
   if ($zip->open($zip_file) === TRUE)
    {
    $zip->extractTo('./assets/gallery/photos/import_zip/'.url_title($get_cat_name));
    $zip->close();
    }
   else
    {
    echo "Failed to extract files from zip archive.";
    }
   
   #Get an array of extracted files from the temp import folder.
   $extracted_files = directory_map('assets/gallery/photos/import_zip/' . url_title($get_cat_name));
   
   #loop through the results.
   foreach($extracted_files as $row => $value)
    {
    #Create Thumbnail Images.
    $config['image_library'] = 'GD2';
    $config['source_image'] = 'assets/gallery/photos/import_zip/' . url_title($get_cat_name).'/'.$value;
    $config['new_image'] = 'assets/gallery/photos/'.url_title($get_cat_name).'/thumbs/'.$value;
    $config['create_thumb'] = TRUE;
    $config['maintain_ratio'] = TRUE;
    $config['quality'] = $this->config->item('thumbnail_quality');
    $config['width'] = $this->config->item('thumbnail_maxwidth');
    $config['height'] = $this->config->item('thumbnail_maxheight');
    $config['thumb_marker'] = '';
    $this->image_lib->initialize($config);
    $this->image_lib->resize();
    
    #config2
    $config2['image_library'] = 'GD2';
    $config2['source_image'] = 'assets/gallery/photos/import_zip/' . url_title($get_cat_name).'/'.$value;
    $config2['new_image'] = 'assets/gallery/photos/'.url_title($get_cat_name).'/normal/'.$value;
    $config2['create_thumb'] = TRUE;
    $config2['maintain_ratio'] = TRUE;
    $config2['quality'] = $this->config->item('normal_quality');
    $config2['width'] = $this->config->item('normal_maxwidth');
    $config2['height'] = $this->config->item('normal_maxheight');
    $config2['thumb_marker'] = '';
    $this->image_lib->initialize($config2);
    $this->image_lib->resize();
    
    #Insert file info into database
    $userfile = $value;
    $this->gallery_model->insert_image_by_zip($userfile);
    }
    #Optimize the Database Table
    $this->load->dbutil();
    $this->dbutil->optimize_table('gallery_photos');
    
    #We will now do garbage collection and delete all the extra junk created during this import.
    delete_files('assets/gallery/photos/import_zip/', TRUE);
    
    #Okay everything is done lets re-direct back to the category these files were uploaded to.
    redirect('gallery_admin/images/' . $this->input->post('cat_id'));
   }
  }
 }

Okay So you may notice we have configured gallery thumbnail sizes which are loaded from the config file. So that file is below

&lt;?php
if (!defined("BASEPATH")) exit("No direct script access allowed");
$config["normal_maxwidth"] = '1024';
$config["normal_maxheight"] = '768';
$config["normal_quality"] = '90';
$config["thumbnail_maxwidth"] = '100';
$config["thumbnail_maxheight"] = '75';
$config["thumbnail_quality"] = '70';
?&gt;

That way you can customize these settings for whatever projects you use this for. You can also easily extend this to create different sized thumbnails. So if you want to make a 3rd group of sizes you can pretty easy.

The relevant model functions are below. These can be customized for your database of course.

function get_categories()
{
$cat = $this->db->get('gallery_categories');
return $cat;
}

function get_cat_name($catid)
{
$this->db->where('id', $catid);
$cat_name = $this->db->get('gallery_categories');
return $cat_name->result();
}

function insert_image_by_zip($file)
{
$array = array(
 'cat_id' => $this->input->post('cat_id'),
 'userfile' => $file,
 'desc_one' => '',
 'desc_two' => '',
 'sort_id' => '0'
 );
$this->db->set($array);
$this->db->insert('gallery_photos');
}

And lastly the view file below.

<div class="form-horizontal">
&lt;?php echo $error?&gt;
&lt;?php echo form_open_multipart('gallery_admin/import_zip/');?&gt;
 <fieldset>
  <legend>&lt;?php echo $this->lang->line('label_import_zip');?&gt;</legend>

   <div class="control-group">
   <label class="control-label">&lt;?php echo $this->lang->line('label_zip_file');?&gt;</label>
    <div class="controls">
    &lt;input type="file" class="field" name="userfile" value="" /&gt;
    </div>
   </div>

   <div class="control-group">
   <label class="control-label">&lt;?php echo $this->lang->line('label_gallery_category');?&gt;</label>
    <div class="controls">
    <select name="cat_id">
    &lt;?php foreach($vcate->result() as $catname) : ?&gt;
    <option value="&lt;?php echo $catname-&gt;id?&gt;">&lt;?php echo $catname->name?&gt;</option>
    &lt;?php endforeach; ?&gt;
    </select>
    </div>
   </div>
  
   <div class="form-actions">
   &lt;input class="btn btn-primary" type="submit" value="Submit" /&gt;
   </div>
  
 </fieldset>
&lt;?php echo form_close();?&gt;
</div>

Hopefully someone finds this handy. :)

Clone this wiki locally