2016-04-08 10 views
0

PHPを使用してcsvsファイルを作成し、フォルダに入れてからzipします。これは完全に動作しますが、ユーザが自動ダウンロードするために作成したZIPを取得できません。リンクをクリックした後のCSVファイルの自動ダウンロード

EDITは、それが今でリダイレクトを取り出した後に動作

は、しかし、それは、ダウンロードされたときに、これを強制する方法はあり.zipの拡張子が欠けていますか?

私のジッパーがrootここ

でジッパー/に格納されている。ここに私のコード

// Add the folder we just created to a zip file. 
$zip_name = 'groups_' . time(); 
$zip_directory = '/zips'; 
$zip = new zip($zip_name, $zip_directory); 
$zip->add_directory('group-csvs'); 
$zip->save(); 

$zip_path = $zip->get_zip_path(); 

header("Content-Description: File Transfer"); 
header("Content-type: application/zip"); 
header("Content-Disposition: attachment; filename=" . $zip_name . ""); 
header("Content-Length: " . filesize($zip_path)); 

readfile($zip_path); 

は私のzipクラスのGetパス方式とコンストラクタ

public function __construct($file_name, $zip_directory) 
    { 
     $this->zip = new ZipArchive(); 
     $this->path = dirname(__FILE__) . $zip_directory . $file_name . '.zip'; 
     $this->zip->open($this->path, ZipArchive::CREATE); 
    } 

    /** 
    * Get the absolute path to the zip file 
    * @return string 
    */ 
    public function get_zip_path() 
    { 
     return $this->path; 
    } 

Iドンです何らかのエラーが発生した場合は、ジップを作成しても何も起こりません。

+0

'$ zip-> save()'は元の 'ZipArchive'クラスの' close() 'を呼び出していますか? –

+0

はいpublic function save() { $ this-> zip-> close(); } – pocockn

答えて

0

.zip extファイル名の末尾に: $zip_name = 'groups_' . time() . '.zip'; カスタムzipクラスで使用している拡張子が、メインファイルの変数に追加されていません。

関連する問題