PHP创建和下载zip,没有错误但不起作用

时间:2017-12-25 18:59:39

标签: php

function downloadZipFunction() {
    echo 'entered downloadzipfunction';
    //$files = $myArray;
    global $files;
    print_r($files); echo 'plis rch here';
    # create new zip opbject
    $zip = new ZipArchive();
    echo "before tempnam";
    # create a temp file & open it
    $tmp_file = tempnam('./docs/','tmp');
    echo "after tempnam";
    echo $tmp_file;
    $zip->open($tmp_file, ZipArchive::CREATE);

    # loop through each file
    foreach($files as $file){
        echo "entered foreach";
        # download file
        $download_file = file_get_contents("docs/".$file);
        echo "docs/".$file;
        #add it to the zip
        $zip->addFromString(basename($file),$download_file);
        echo basename($file);
    }

    # close zip
    $zip->close();

    # send the file to the browser as a download
    header('Content-disposition: attachment; filename= docs_$prop.zip');
    header('Content-type: application/zip');
    readfile($tmp_file);
    echo $tmp_file;
    //unlink($tmp_file);
}

输出'输入downloadzipfunctionArray([0] => 1514223591754.xml)plis rch here'但在此之后别无其他,没有错误。 服务器上也没有创建文件。

1 个答案:

答案 0 :(得分:0)

你可以看一下这个例子 它来自php.net

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
    $zip->addFromString('test.txt', 'file content goes here');
    $zip->addFile('data.txt', 'entryname.txt');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>