user6689924 user6689924 - 1 month ago 13
PHP Question

Codeigniter output filename in zip and rar file

i seen zipArchive class, but i dont know how to apply it in codeigniter.

is there any library in codeigniter that can output all file names inside the compressed file?

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Tools extends CI_Controller {

public function index()
{
//$this->load->library('zip');

$path = base_url()."files/2016_02_26_02_03_26_Philosophizing_and_Being_Human_TB.rar";
$zip = new ZipArchive;
if ($zip->open($path))
{
for($i = 0; $i < $zip->numFiles; $i++)
{
echo 'Filename: ' . $zip->getNameIndex($i) . '<br />';
}
}
else
{
echo 'Error reading zip-archive!';
}
}


}

Answer

You may use following code to accomplish this task. You should use RarArchive class instead of what are using now. Because your file in not zip but .rar

   $rar_arch = RarArchive::open('./files/2016_02_26_02_03_26_Philosophizing_and_Being_Human_TB.rar');
    if ($rar_arch === FALSE)
        die("Could not open RAR archive.");

    $rar_entries = $rar_arch->getEntries();
    if ($rar_entries === FALSE)
        die("Could not retrieve entries.");

    //echo "Found " . count($rar_entries) . " entries.\n";

    foreach ($rar_entries as $e) {
        echo $e.'<br/>';
        //echo "\n";
    }
    $rar_arch->close();

But before this make sure your RarArchive extension is installed in your server. For checking whether it is enabled, you may use phpinfo() and search RAR support

For ZIP use this

$zip = new ZipArchive; 
        if ($zip->open('./files/2016_02_26_02_03_26_Philosophizing_and_Being_Human_TB.zip') == TRUE) {
            for ($i = 0; $i < $zip->numFiles; $i++) {
                $filename = $zip->getNameIndex($i);
                echo $filename.'<br/>';
            }
        }
Comments