通过压缩下载多个文件

时间:2019-02-12 17:18:06

标签: php download

我的网站上有多个文件,并希望使其能够通过压缩文件然后再下载来下载所有这些文件。我想在服务器上进行压缩,所以我搜索了一些解决方案,发现this one唯一的问题是:这是一个没有任何html的php文件,我也不知道如何将其实现为我的网页我对php没有经验。

这是我网站的代码:

<?php include
        '/home/www/***/***/***.php' ;
?>
<title>Downloads</title>
<style>
        a:hover {
            text-decoration: none;
        }
</style>
<?php include
        '/home/www/***/***/***.php' ;
?>
    <div id="site" class="container">
        <div class="row">
                <div class="col-sm-12">
                        <h1><strong>Downloads</strong></h1>
                        <p>Welcome to this private page of mine, dedicated just for files too big to send via e-mail.<br>
                        Hope you are able to find what you need. If not, mail me: <a href="mailto:***@***?Subject=Question" target="_top">***@***</a>.</p>
                </div>
        </div>
        <br>
        <a href="files/***.txt" download="***.txt">
    <div class="box downloadbox">
        <div class="row">
        <div class="col-sm-2 col-md-1 col-lg-2">
            <img id="plaatjes" src="img/***/***/***.png" alt="-">
        </div>
                <div class="col-sm-10 col-md-11 col-lg-10">
            <div class="series">
                                <br>
                                <br>
                <h2><strong>Watch List</strong></h2>
            </div>
        </div>
    </div>
        </div>
        </a>
        <br>
    </div>
<?php include
        '/home/www/***/***/***.php' ;
?>

这是压缩php文件的代码:

<?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");
    }
    //add each files of $file_name array to archive
    foreach($file_names as $files)  {
        $zip->addFile($file_path.$files,$files);        
    }
    $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 (some servers need this option)
    exit;   
  }
if(isset($_POST['formSubmit'])) {
//$file_names=$_POST['items'];// Always sanitize your submitted data!!!!!!
//$file_names = filter_var_array($_POST['items']);//works but it's the wrong method
$filter = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS) ;
$file_names = $filter['items'] ; 
//Archive name
$archive_file_name='DEMO-archive.zip';
//Download Files path
$file_path= getcwd(). '/content/';
//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;
}

我知道这有点混乱,也许我没有问正确的问题,但我仍然希望有人能帮助我!

0 个答案:

没有答案