如何解压缩上传的zip文件?

时间:2012-07-04 08:31:19

标签: php codeigniter unzip

我正在尝试使用带有以下代码的codeigniter框架上传压缩文件

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);

    }
}

但主要的问题是,当调用提取函数时,它会提取zip但结果是空文件夹。有没有办法克服这个问题?

6 个答案:

答案 0 :(得分:3)

$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();

答案 1 :(得分:0)

试试这个:

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

答案 2 :(得分:0)

嗯..,我认为您设置了上传的zip文件的错误路径或您的目标路径('./application/modules/')不正确。

试试这个:

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

我用这个 - &gt; $ data ['upload_data'] ['full_path'] ,以确保它是上传文件的真实路径。

希望有所帮助:)

答案 3 :(得分:0)

同样的问题我几乎没有回来。如果你仔细观察,你会发现 请复制zip文件并粘贴到包含programe文件(.php)的文件夹之后

我认为文件不存储在临时文件夹中。

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();
}` 

答案 4 :(得分:0)

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);
    }
  }
}

答案 5 :(得分:0)

如果有人来这里提出同样的问题,只需添加chmod($ file,0777);到原来的代码发布了另一个。这解决了空文件的问题。