Thursday, 28 May 2015

Learning ... Comments

Learn to do things in diffent ways because you learn from doing things differently,
I strongly believe that it is become known knowledge that the best way to teach is by video rather than text.
Anyway here is what I did today, basically it store 1 or more text files into a zip the code its below

<?php
require_once 'classes/zipper.php';
$zipper= new Zipper;
$zipper->add('files/one.txt');
$zipper->add(array('files/two.txt','files/three.txt'));
$zipper->store('files/font.zip');


class Zipper {
private $_files= array(),
$_zip;

public function __construct (){
$this->_zip = new ZipArchive;
}


public function add($input) {
if(is_array($input)){
$this->_files=array_merge($this->_files, $input);
}
else {
$this ->_files[]=$input;
}
}
public function store($location = null){
if(count($this->_files)&&$location){
foreach ($this->_files as $index => $file){
if(!file_exists($file)){
unset($this->_files[$index]);
}
}

if($this->_zip->open($location,file_exists($location) ? ZIPARCHIVE::OVERWRITE :ZIPARCHIVE ::CREATE)){
foreach($this->_files as $file){
$this->_zip->addFile($file,$file);
}
$this->_zip->close();
}
}
}
}

No comments:

Post a Comment