2013-05-09 17 views
0

我想使文件的下載腳本,收集所有的文件並下載文件已不是所有的目錄結構的該集合zip文件..不想目錄結構與ZipArchive

我有一個文件在下載/文件夾1 /文件夾1/filename.extension

這裏是我的PHP代碼:

if(!extension_loaded('zip')){ 
    echo "<script>alert('Error: Please contact to the Server Administrator!');</script>"; 
    exit; 
} 

$zip = new ZipArchive; 
if($zip->open($zipname, ZipArchive::OVERWRITE) === TRUE){ 
    foreach($files as $file){ 
     $zip->addFile(BASE_PATH.$file_path.'/'.$file, $file); 
    } 
    $zip->close(); 
} else { 
    echo "<script>alert('Error: problem to create zip file!');</script>"; 
    exit; 
} 

這段代碼給了我這樣的結構:

enter image description here

它給WAMP的完整路徑(包括路徑導演和文件)和文件,我只是想添加的文件不是目錄..

誰能告訴我什麼,我錯過了?

回答

0

每次下載鏈接帶有獨特的下載密鑰,我只是download.zip文件和工作的名稱添加unique_key ...

// $db_secret_key Random Unique Number 
$zipname = $db_secret_key."_download.zip"; 

if(!extension_loaded('zip')){ 
    echo "<script>alert('Error: Please contact to the Server Administrator!');</script>"; 
    exit; 
} 


$zip = new ZipArchive; 
if($zip->open($zipname, ZipArchive::CREATE) === TRUE){ 
    foreach($files as $file){ 
      $zip->addFile(BASE_PATH.$file_path.'/'.$file, $file); 
    } 
    $zip->close(); 

    // Force Download 
    header("Content-Type: application/zip"); 
    header("Content-disposition: attachment; filename=$zipname"); 
    header("Content-Length: ".filesize($zipname).""); 
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile($zipname); 
    exit; 
} else { 
    echo "<script>alert('Error: problem to create zip file!');</script>"; 
    exit; 
}