【问题标题】:how to unzip uploaded zip file?如何解压上传的 zip 文件?
【发布时间】:2012-07-04 17:38:09
【问题描述】:

我正在尝试使用带有以下代码的 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,但结果是空文件夹。有什么办法可以解决这个问题吗?

【问题讨论】:

标签: php codeigniter unzip


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案2】:

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

    试试这个:

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

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

    希望对你有帮助:)

    【讨论】:

      【解决方案3】:

      我在几分钟后遇到了同样的问题。如果你仔细观察你会发现 请复制zip文件并粘贴到包含程序文件(.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】:
      $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();
      

      【讨论】:

        【解决方案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】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-03-31
            • 1970-01-01
            • 2011-04-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-07-05
            相关资源
            最近更新 更多