0

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

4

6 回答 6

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();
于 2012-10-20T10:20:43.677 回答
0

尝试这个 :

<?php
exec('unzip filename.zip');
?>
于 2012-07-04T09:03:01.360 回答
0

我遇到了同样的问题几分钟。如果你仔细观察你发现请复制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();
}` 
于 2012-10-01T09:50:10.343 回答
0

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

尝试这个 :

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

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

希望能帮助到你 :)

于 2012-07-04T09:50:40.743 回答
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);
    }
  }
}
于 2015-05-25T12:24:49.180 回答
0

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

于 2018-06-11T02:39:26.560 回答