使用php

时间:2019-03-16 19:15:02

标签: php zip

我有一个正在请求数据的Web服务。接收到的数据存储在变量$response中。

之后,我正在创建文件来存储数据。

然后,我尝试将这些文件添加到zip中,然后再下载该zip。我已经咨询了与我类似的各种问题和答案,但仍然无法解决我的问题。请注意,我已经安装了zip库。

以下是我的php脚本:

// Load zip library
$zip = new ZipArchive();

//Zip name
$zip_name = "data.zip";

if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){ 
    // Opening zip file to load files
    $error = "ZIP creation failed at this time";
}
else{
    echo "zip creation successful";
}

function myFunc(){
    //Some codes
     global $zip;

     $myfile = fopen($filepath, "w");

     fwrite($myfile, $response);
     fclose($myfile);

     echo "test1";
     try {
        //add file to zip
        $zip->addFile($myfile);
     }
     catch (Exception $e) {
        echo $e->getMessage();
     }
     echo "test2";
}

$zip->close();

//Some codes    

if(file_exists($zip_name)){
    // push to download the zip
    header('Content-type: application/zip');
    header('Content-Disposition: attachment; filename="'.$zip_name.'"');
    readfile($zip_name);

    // remove zip file is exists in temp path
    unlink($zip_name);
}

运行上述脚本后,将显示 test1 ,而不是 test2 。我认为问题出在$zip->addFile($myfile);这一行。

从以前的帖子中,我可以注意到函数addFile具有2个参数。我该如何为此编辑代码?

感谢您的帮助。

0 个答案:

没有答案
相关问题