如何使用生成的文件列表添加到zip csv文件中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用生成的文件列表添加到zip csv文件中相关的知识,希望对你有一定的参考价值。

我有关于使用php脚本下载文件并将其添加到zip的问题。我现在使用这个脚本

<?php include "zahlavi.php"; ?>
<?php include_once('assets/bs4-paginator.class.php');  ?>
<?php include ('assets/config.php');  ?>
<!doctype html>
<html lang="en-US" xmlns:fb="https://www.facebook.com/2008/fbml"         
xmlns:addthis="https://www.addthis.com/help/api-spec"  prefix="og: http://ogp.me/ns#" class="no-js">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Generator</title>

<link rel="shortcut icon" href="https://demo.learncodeweb.com/favicon.ico">
<link rel="stylesheet"     
href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384- 
GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" 
 integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" 
crossorigin="anonymous">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->

<!-- Global site tag (gtag.js) - Google Analytics -->

</head>
<body>

</div>




<div class="container">


    <div class="panel panel-default">
        <div class="panel-heading clearfix"><i class="fa fa-fw fa-globe"></i> 
    <strong>Prohlížení</strong>
        <div class="panel-body">
            <div class="col-sm-12">
                <h5><i class="fa fa-fw fa-search"></i>Hledat PM</h5>
                <form method="get">
                    <div class="row">
                        <div class="col-sm-2">
                            <div class="form-group">
                                <label>Stav</label>
                                <input type="text" name="zak_stav" id="zak_stav" class="form-control" 
 value="<?php echo isset($_REQUEST['zak_stav'])?$_REQUEST['zak_stav']:''?>" placeholder="Zadat 
  stav...">
                            </div>
                        </div>
                        <div class="col-sm-2">
                            <div class="form-group">
                                <label>Datum ukončení el</label>
                                <input type="text" name="zak_termin_ukonceni_elektrina" id="zak_termin_ukonceni_elektrina" class="form-control" value="<?php echo isset($_REQUEST['zak_termin_ukonceni_elektrina'])?$_REQUEST['zak_termin_ukonceni_elektrina']:''?>" placeholder="Zadat termin ukončení..">
                            </div>
                        </div>
                        <div class="col-sm-2">
                            <div class="form-group">
                                <label>Datum ukončení pl</label>
                                <input type="text" class="tel form-control" name="zak_termin_ukonceni_plyn" id="zak_termin_ukonceni_plyn" x-autocompletetype="tel" placeholder="Zadat termín ukončení.." value="<?php echo isset($_REQUEST['zak_termin_ukonceni_plyn'])?$_REQUEST['zak_termin_ukonceni_plyn']:''?>">
                            </div>
                        </div>
                        <div class="col-sm-4">
                            <div class="form-group">
                                <label>&nbsp;</label>
                                <div>
                                    <button type="submit" name="submit" value="search" id="submit" class="btn btn-primary"><i class="fa fa-fw fa-search"></i> Hledat</button>
                                    <a href="<?php echo $_SERVER['PHP_SELF'];?>" class="btn btn-danger"><i class="fa fa-fw fa-sync"></i> Vyčistit filtr</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>


            <hr>
            <div class="table table-responsive">
                <form method="post" action="createzip.php">
                    <table class="table table-striped table-bordered table-hover">
                        <thead>
                            <tr class="bg-primary text-white">
                                <th width="25">id#</th>
                                <th>nazev pm</th>
                                <th>Jmeno</th>
                                <th>Prijmeni</th>
                                <th>Mesto</th>
                                <th>Ulice</th>
                                <th>Datum narozeni</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $condition  =   '';
                            if(isset($_REQUEST['zak_stav']) and $_REQUEST['zak_stav']!="")
                            $condition  .=  ' AND zak_stav LIKE "%'.$_REQUEST['zak_stav'].'%" ';
                            
                            if(isset($_REQUEST['zak_termin_ukonceni_elektrina']) and $_REQUEST['zak_termin_ukonceni_elektrina']!="")
                            $condition  .=  ' AND zak_termin_ukonceni_elektrina LIKE "%'.$_REQUEST['zak_termin_ukonceni_elektrina'].'%" ';
                            
                            if(isset($_REQUEST['zak_termin_ukonceni_plyn']) and $_REQUEST['zak_termin_ukonceni_plyn']!="")
                            $condition  .=  ' AND zak_termin_ukonceni_plyn LIKE "%'.$_REQUEST['zak_termin_ukonceni_plyn'].'%" ';
                            

                            $db         =   new mysqli('','','','');
                            mysqli_set_charset($db,"utf8");
                            $pages->default_ipp =   15;
                            $fileQry    =   $db->query("SELECT * FROM zakaznici WHERE 1 ".$condition." ORDER BY id_tabulky DESC LIMIT 30");

                            $pages->items_total =   count($fileQryql);
                            $pages->mid_range   =   9;
                            $pages->paginate(); 


                            if($fileQry->num_rows>0)
                                $s      =   '';
                                while($row  =   $fileQry->fetch_assoc())
                                    $s++;
                            ?>
                            <tr>
                                <td><?php echo $s;?></td>
                                <td>
                                    <div class="custom-control custom-checkbox mb-3">
                                        <input type="checkbox" name="fileId[]" class="custom-control-input" id="checkbox<?php echo $row['id_tabulky']?>" value="<?php echo $row['id_tabulky']?>">
                                        <label type="hiden" class="custom-control-label" for="checkbox<?php echo $row['id_tabulky']?>"><?php echo $row['zak_soubor_pm'];?></label>
                                    </div>
                                </td>
                                <td>
                                <?php echo $row['zak_jmeno'];?>
                                </td>

                                <td>
                                <?php echo $row['zak_prijmeni'];?>
                                </td>

                                <td>
                                <?php echo $row['zak_mesto'];?>
                                </td>

                                <td>
                                <?php echo $row['zak_ulice'];?>
                                </td>

                                <td>
                                <?php echo $row['zak_datum_narozeni'];?>
                                </td>


                            </tr>
                                <?php 
                                
                             ?>
                            <tr>
                                <td colspan="2"><button type="submit" name="createzip" id="createzip" value="createzip" class="btn btn-primary"><i class="fa fa-archive"></i> Stáhnout vybrané PM</button></td>
                            </tr>
                        </tbody>
                    </table>
                </form>
            </div>
        </div>

    </div>
</div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
<script>
    $(document).ready(function()
        $('input[type="file"]').on('change', function () 
            let filenames = [];
            let files = document.getElementById('customFile').files;
            if(files.length>1)
                filenames.push('Total Files ('+files.length+')');
            else
                for (let i in files) 
                    if (files.hasOwnProperty(i)) 
                        filenames.push(files[i].name);
                    
                
            
            $(this).next('.custom-file-label').html(filenames.join(','));
        );
    );
</script>
</body>
</html>
<?php include "zapati.php"; ?>

用于从数据库下载文件。脚本效果很好

但是我需要添加到此zip csv或excel文件中,其文件名在具有已导出行ID的行中具有完整路径,并具有来自DB等客户名称的更多信息...

我该怎么做?如何从此mysql表将CSV导出添加到带有文件的此生成的zip中?

Createzip.php包含在这里:

<?php
 if(isset($_REQUEST['createzip']) and $_REQUEST['createzip']!="")
extract($_REQUEST);

$filename   =   'temp/vygenerovane-pm.zip';

$db         =   new mysqli('','- 
 ','','');
mysqli_set_charset($db,"utf8");
$fileQry    =   $db->query('SELECT * FROM zakaznici WHERE id_tabulky IN 
  ('.implode(",",$fileId).')');

$zip = new ZipArchive;
if ($zip->open($filename,  ZipArchive::CREATE))
    while($row  =   $fileQry->fetch_assoc())
        $zip->addFile(getcwd().'/'.'/'.$row['zak_soubor_pm'], 
 $row['zak_soubor_pm']);
    


    $zip->close();

    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$filename");
    header("Content-length: " . filesize($filename));
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$filename");
    unlink($filename);
else
   echo 'Failed!';


?>
答案

已解决。在下载zip之前,我先创建一个csv文件,并将其保存到temp中,然后再链接到ZIP中,并在临时删除下载文件之后。

以上是关于如何使用生成的文件列表添加到zip csv文件中的主要内容,如果未能解决你的问题,请参考以下文章

即时将 csv 文件添加到 zip 文件以进行导出和导入

在 Python 中将 zip 列表保存到 csv

当列表包含某些内容时,为啥使用 zip() 只写入 CSV 文件?

如何读取多个 zip 文件中的所有 csv 文件?

如何提取多个 zip 文件并在 R 中读取这些 csv? [复制]

如何将上传的文件添加到Vue中已上传的文件列表中?