dongqian1893 2014-07-19 13:41
浏览 61

PHP:从目录创建动态zip文件并从href链接下载

I create zip file from directory like this :

<?php // On instancie la classe.
        $zip = new ZipArchive();

        if($zip->open($cheminAlbum.'.zip', ZipArchive::CREATE) == TRUE)
        {

         $fichiers = scandir($cheminAlbum.'/');
        // On enlève . et .. qui représentent le dossier courant et le dossier parent.
         unset($fichiers[0], $fichiers[1]);

        foreach($fichiers as $f) {
                if(!$zip->addFile($cheminAlbum.'/'.$f, $f)) {
                    echo 'Impossible d&#039;ajouter &quot;'.$f.'&quot;.<br/>';
                } else {
                    echo 'Fichier ajouter &quot; : '.$f.'&quot;.<br/>';
                }
         }

        $zip->close();

        } else {
            echo 'Erreur, impossible de créer l&#039;archive.';
    }

 ?>

I add link for download it like this :

<a href="<?php echo $cheminAlbum.'.zip';?>"> Download Album </a>

When i access to directory, i dont found the zip file after call php page And when i click to the link, the file doesnt exist

The file is add correctly to the zip when i debug (add echo)

what i forgot in y code ?

  • 写回答

3条回答 默认 最新

  • douchenhui5569 2014-07-19 13:59
    关注

    The Problem is with the file name. your code is creating a foldername/.zip file instead of foldername/album1.zip check the below code add a dynamic filename in url and when opending

    when creating and providing access to files you need to consider two things. when creating file you need to use File System path and when you are providing link you need to provide url of the file

    <?php // On instancie la classe.
            $zip = new ZipArchive();
            $filename = "somealbumName";
            if($zip->open($cheminAlbum.'/'.$filename.'.zip', ZipArchive::CREATE) == TRUE)
            {
    
             $fichiers = scandir($cheminAlbum.'/');
            // On enlève . et .. qui représentent le dossier courant et le dossier parent.
             unset($fichiers[0], $fichiers[1]);
    
            foreach($fichiers as $f) {
                    if(!$zip->addFile($cheminAlbum.'/'.$f, $f)) {
                        echo 'Impossible d&#039;ajouter &quot;'.$f.'&quot;.<br/>';
                    } else {
                        echo 'Fichier ajouter &quot; : '.$f.'&quot;.<br/>';
                    }
             }
    
            $zip->close();
    
            } else {
                echo 'Erreur, impossible de créer l&#039;archive.';
        }
    
     ?>
     <a href="<?php echo $cheminAlbum.'/'.$filename.'.zip';?>"> Download Album </a>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)