2012-07-04 98 views
0

我想上传使用笨框架压缩文件与下面的代码如何解压上传的zip文件?

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但结果是空文件夹。有什么办法可以解决这个问题吗?

+0

见[zip_open]文件(http://www.php.net/manual/en/function.zip-open.php)方法 –

回答

0

试试这个:

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

嗯..我想你设定上传的zip文件的或不正确的路径的目标路径(“./application/modules/”)不正确。

试试这个:

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

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

希望它能帮助:)

0

我面对back.if你观察到几个分钟同样的问题,你仔细找 请复制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(); 
}` 
+1

能否请你完成你的答案 - 似乎你已经停止在中期写作...... – Spontifixus

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