2013-02-06 84 views
0

我已經PHP腳本創建的一些文本文件如何提供下載鏈接已經壓縮文件在PHP

<?php 
    $path = "path\to\files"; 
    $files = array($path.'\ionic interaction.txt',$path.'\file1.txt',$path.'\file2.txt',$path.'\file3.txt',$path.'\file3.txt'); 
    $folder = $_POST['filename']; 
    $zipname = $folder.'.zip'; 
    echo '<form action="download.php" method="post" name="zippass2download"><input type="hidden" name="zipname" value="'.$zipname.'"></form>'; 
    $zip = new ZipArchive; 
    $zip -> open($zipname, ZipArchive::CREATE); 
    foreach($files as $file) 
    { 
    $zip -> addFile($file); 
    } 
    $zip -> close(); 
    ?> 

下一步該腳本我創建了一個鏈接下載創建的zip文件夾壓縮文件夾在第一個腳本中。

<a href="download.php">Download</a> 

在的download.php我下面的代碼

<?php 
$path2zip = 'C:/wamp/www/PPInt'; 
$tobezipped = $_POST['zipname']; 
header('Content-disposition: attachment; filename=$tobezipped'); 
header('Content-type: application/zip'); 
readfile('$path2zip/$tobezipped'); 
?> 

沒有得到哪裏出了問題。任何人都可以解決這個問題嗎?

+0

what echo'$ variable';將會?看[This](http://php.net/manual/en/language.types.string.php) – 2013-02-06 07:32:18

+0

我沒有看到任何提交'

' –

回答

1

$ tobezipped = $ _POST ['zipname'];

沒有在調用download.php時的get請求中設置。在生成文件後,Rember HTTP是無狀態的。當鏈接被點擊時,POST PARAMS不會生活在下一個GET請求中。

你可能需要將zipname /路徑添加到您的download.php鏈接作爲 「的download.php?tobezipped = zipname.zip」

0

試試下面的代碼

<?php 
$file_name = $_POST['zipname']; 
$file_url = 'C:/wamp/www/PPInt'. $file_name; 
header('Content-Type: application/octet-stream'); 
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"".$file_name."\""); 
readfile($file_url); 
?> 
1

「下載.php「沒有得到」$ _POST ['zipname']「,因爲你沒有發佈你的表單,而是你給的鏈接,嘗試給按鈕張貼表單,你有隱藏的輸入文件名。 下面的「Dasun」給出的代碼的另一個工作是檢查文件和zip文件的相對URL。

相關問題