使用php将文件添加到zip并下载zip

I have a web service that is requesting for data. The data received is being stored in the variable $response.

After that, i am creating files to store the data.

Then i am trying to add these files to a zip and afterwards download that zip. I have consulted various questions and answers similar to mine but i still cannot solve my problem. Note that i have the zip library installed.

Below is my php script:

// 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);
}

After running the above script, test1 is being displayed and not test2. I think that the problem lies in this line $zip->addFile($myfile);.

From previous posts, i could notice that the function addFile takes 2 parameters. How can i edit my code for this?

Thanks for your help.