2011-07-25 63 views
1

我正在創建一個PHP腳本,它將從一個位置創建一個zip文件,該位置將是通過$ _GET傳遞的變量。該位置將是一個文件夾,然後將被壓縮,用戶將被提示下載該文件夾,下載後該文件夾將需要自動刪除。創建一個ZIP文件下載,然後將其刪除

任何人都可以幫忙嗎?

感謝

+1

你需要什麼部分幫助?你試過去[PHP手冊](http://php.net)的Zip/ZipArchive章節嗎?一個StackOverflow問題應該是一個具體的編程問題,具體的答案,而不是你想寫的程序的描述。 –

+0

實際上,您應該可以在內存中構建zip文件,而不是先將其保存到磁盤,然後再次將其刪除。雖然這取決於大小。 –

+0

請參閱此類似的問題答案:http://stackoverflow.com/questions/5603851/how-to-create-a-zip-file-using-php-and-delete-it-after-user-downloads-it/17399319 #17399319 [此處輸入鏈接的描述] [1] [1]:http://stackoverflow.com/questions/5603851/how-to-create-a-zip-file-using-php-and -delete-it-user-downloads-it/17399319#17399319 –

回答

4

http://php.net/manual/en/ref.zip.php

你可以做這樣的事情:

  • 驗證得到
  • 查找,如果該文件夾存在
  • 壓縮的文件夾
  • 閱讀新創建的zip文件並將其刪除最後,像這樣:

    header('Content-Description: File Transfer'); 
        header('Content-Type: application/octet-stream'); 
        header('Content-Disposition: attachment; filename='.basename($file)); 
        header('Content-Transfer-Encoding: binary'); 
        header('Expires: 0'); 
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
        header('Pragma: public'); 
        header('Content-Length: ' . filesize($file)); 
        ob_clean(); 
        flush(); 
        readfile($file); 
        @unlink($file); 
    

代碼採取弗羅姆在這裏:http://php.net/manual/en/function.readfile.php

+0

儘管問題措辭不佳,但這只是我尋找的解決方案,而且它的工作原理! +1 –

相關問題