how to unzip uploaded zip file?

帅比萌擦擦* 提交于 2019-12-13 03:56:29

问题


I am trying to upload a zipped file using codeigniter framework with following code

function do_upload()
{
    $name=time();
    $config['upload_path'] = './uploadedModules/';
    $config['allowed_types'] = 'zip|rar';
    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_view', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());

        $this->load->library('unzip');

    // Optional: Only take out these files, anything else is ignored
    $this->unzip->allow(array('css', 'js', 'png', 'gif', 'jpeg', 'jpg', 'tpl', 'html', 'swf'));

     $this->unzip->extract('./uploadedModules/'.$data['upload_data']['file_name'], './application/modules/'); 



        $pieces = explode(".", $data['upload_data']['file_name']);
        $title=$pieces[0];
        $status=1;
        $core=0;
        $this->addons_model->insertNewModule($title,$status,$core);

    }
}

But the main problem is that when extract function is called, it extract the zip but the result is empty folder. Is there any way to overcome this problem?


回答1:


$zip = new ZipArchive;

     $res = $zip->open($fileName);

    if($res==TRUE)
        {  
            $zip->extractTo($path.$fileName);

            echo "<pre>";
            print_r($zip);//to get the file type


            $zip->close();



回答2:


try this :

<?php
exec('unzip filename.zip');
?>



回答3:


Hmm.., I think you set an incorrect path of your uploaded zip file OR your destination path ('./application/modules/') is incorrect.

Try this :

$this->unzip->extract($data['upload_data']['full_path'], './application/modules/');

I use this -> $data['upload_data']['full_path'], to make sure that it's a real path of the uploaded file.

Hope it helps :)




回答4:


same problem i faced few min back.if you observe carefully you find please copy zip file and paste to folder contain programe file(.php) after that you

i think file is not store in temp folder.

if(preg_match("/.(zip)$/i", $fileName))
{
$moveResult= move_uploaded_file($fileTmpLoc, $fileName);

if($moveResult == true)
{ 
     $zip = new ZipArchive;

     $res = $zip->open($fileName);

    if($res==TRUE)
        {  
            $zip->extractTo($path.$fileName);

            echo "<pre>";
            print_r($zip);


            $zip->close();
        } else {
         echo 'failed';
        }

}

unlink($fileName); // Remove the uploaded file from the PHP temp folder
//exit();
}` 



回答5:


class Upload extends CI_Controller {
function __construct(){
    parent::__construct();
    // load ci's Form and Url Helpers
    $this->load->helper(array('form', 'url'));
}
function index(){
    $this->load->view('upload_form_view', array('error' => ' ' ));
}
function file_upload(){
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'zip';
    $config['max_size'] = '';
    $this->load->library('upload', $config);
  if ( ! $this->upload->do_upload()){
    $error = array('error' => $this->upload->display_errors());
    $this->load->view('upload_form_view', $error);
  }else{
    $data = array('upload_data' => $this->upload->data());
    $zip = new ZipArchive;
    $file = $data['upload_data']['full_path'];
    chmod($file,0777);
    if ($zip->open($file) === TRUE) {
            $zip->extractTo('./uploads/');
            $zip->close();
            echo 'ok';
    } else {
            echo 'failed';
    }
    $this->load->view('upload_success_view', $data);
    }
  }
}



回答6:


In case anyone comes here for same question, just add chmod($file,0777); to the original code posted yetAnotherSE. That solves the issue of empty files.



来源:https://stackoverflow.com/questions/11325310/how-to-unzip-uploaded-zip-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!