php添加文件以压缩返回空白文件

时间:2018-03-08 07:44:02

标签: php

我遇到以下代码的问题(它在本地工作正常)。

我使用此图像库来使用复选框下载一系列图像。无论如何,这在我的本地主机上运行正常但是当我在我的实际站点上执行此代码时,它会下载一个包含正确文件名的zip文件,但文件是空白的。

对于为什么会发生这种情况的任何建议?

<?php

function zipFilesAndDownload($file_names, $archive_file_name, $file_path)
{
    $zip = new ZipArchive();
    //create the file and throw the error if unsuccessful
    if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE) !== true) {
        exit("cannot open <$archive_file_name>\n");
    }

    foreach ($file_names as $files) {
        # download file
        $download_file = file_get_contents($files);

        #add it to the zip
        $zip->addFromString(basename($files), $download_file);
    }

    $zip->close();
    $zipped_size = filesize($archive_file_name);
    header("Content-Description: File Transfer");
    header("Content-type: application/zip");
    header("Content-Type: application/force-download");// some browsers need this
    header("Content-Disposition: attachment; filename=$archive_file_name");
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header("Content-Length:" . " $zipped_size");
    ob_clean();
    flush();
    readfile("$archive_file_name");
    unlink("$archive_file_name"); // Now delete the temp file on the server
    exit;
}

if (isset($_POST['formSubmit'])) {
    //$file_names=$_POST['files'];// 
    //$file_names = filter_var_array($_POST['files']);//works but it's the wrong method
    $filter = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS); //sanitise files
    $file_names = $filter['files'];
    //Archive name
    $archive_file_name = 'product-images.zip';
    //Download Files path
    $file_path = getcwd() . '';
    //cal the function
    zipFilesAndDownload($file_names, $archive_file_name, $file_path);
} else {

    header("Refresh: 5; url= ./index.php ");
    print '<h1 style="text-align:center">You you shouldn\'t be here ......</pre>
    <p style="color: red;"><strong>redirection in 5 seconds</strong></p>
    <pre>';
    exit;
}
?>

编辑:我在我的php错误日志中看到了这个:

PHP message: PHP Warning:  ZipArchive::close(): Failure to create temporary file: Permission denied in /var/www/html/wp-content/plugins/jig/download-product-images.php on line 19

0 个答案:

没有答案