2012-07-04 141 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); 
    } 
    } 
}