【问题标题】:Zip files in a directory use part of URL as file name目录中的 Zip 文件使用部分 URL 作为文件名
【发布时间】:2020-07-12 18:54:15
【问题描述】:

我使用一个 php 文件来创建一个文件夹中所有 jpg 文件的 zip 存档并使其可供下载。

这是脚本:

<?php
$zip = new ZipArchive;
$download = 'FileName.zip';
$zip->open($download, ZipArchive::CREATE);
foreach (glob("*.jpg") as $file) { /* Add appropriate path to read content of zip */
    $zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename = $download");
header('Content-Length: ' . filesize($download));
header("Location: $download");
?>

我想知道是否可以使用部分 url 作为存档名称? 我的网址如下所示:

https://www.example.com/data/pictures/album/

我希望存档名称为 Pictures-Album-CustomText.zip

【问题讨论】:

    标签: php zip zipfile


    【解决方案1】:

    你有几个选择:

    选项 1:使用 $_SERVER['PHP_SELF']substr()str_replace() 的组合。

    选项 2:使用 $_SERVER['PHP_SELF']rtrim()explode()count() 的组合。

    第一个选项,细分:


    $url = $_SERVER['PHP_SELF'];              // the current full url
    strrpos($url, "pictures/")                // finds "pictures/" in the $url variable
    substr($url, strrpos($url, "pictures/"))  // extracts everything from "pictures/" onwards
    str_replace("/","-", $name_pre);          // replaces "/" with "-" 
    

    <?php
    
        $url = $_SERVER['PHP_SELF'];
        $name_pre = substr($url, strrpos($url, "pictures/"));
        $name_pre = str_replace("/","-", $name_pre);
        $zip = new ZipArchive;
        $download = $name_pre . 'FileName.zip';
        $zip->open($download, ZipArchive::CREATE);
        foreach (glob("*.jpg") as $file) { 
           $zip->addFile($file);
        }
        $zip->close();
        header('Content-Type: application/zip');
        header("Content-Disposition: attachment; filename = $download");
        header('Content-Length: ' . filesize($download));
        header("Location: $download");
    ?>
    

    第二个选项,细分:

    $url = rtrim($_SERVER['PHP_SELF'], "/"); // get url and remove trailing "/"
    $url_pieces = explode('/', $url);        // break string into pieces based on "/"
    $url_pieces_count = count($url_pieces);  // count the number of pieces
    $name_pre = $url_pieces[($url_pieces_count - 2)] . "-" . $url_pieces[($url_pieces_count - 1)] . "-"; // construct the filename preface
    

    <?php
    
        $url = rtrim("https://www.example.com/data/pictures/album/", "/");
        $url_pieces = explode('/', $url);
        $url_pieces_count = count($url_pieces);
        $name_pre = $url_pieces[($url_pieces_count - 2)] . "-" . $url_pieces[($url_pieces_count - 1)] . "-";
        $zip = new ZipArchive;
        $download = $name_pre . 'FileName.zip';
        $zip->open($download, ZipArchive::CREATE);
        foreach (glob("*.jpg") as $file) { 
           $zip->addFile($file);
        }
        $zip->close();
        header('Content-Type: application/zip');
        header("Content-Disposition: attachment; filename = $download");
        header('Content-Length: ' . filesize($download));
        header("Location: $download");
    ?>
    

    【讨论】:

    • 这很好,但是文件名与我预期的不一样。文件名成为 .com "-data-pictures-album-download.phpFileName.zip" 之后的整个 URL 部分我想要的是 URL "Pictures-Album-FileName.zip" 的最后两部分我相信我需要做一些修剪并改变案例以实现我正在寻找的东西?
    • 我已经定义了两次$name_pre 变量,以清楚地显示代码中发生了什么。您不必像那样分解它。
    • 感谢@Mech,它只需稍作修改即可工作,我在这里发布了最终代码。请让我知道代码是否可以改进?
    • 很高兴我能帮上忙 :)
    【解决方案2】:

    这成为我的最终代码(@Mech 代码 + ucwods),我使用 ucwords 将单词大写 -

    <?php
    
    $url = rtrim($_SERVER['PHP_SELF'], "/"); // get url and remove trailing "/"
    $url_pieces = explode('/', $url);
    $url_pieces_count = count($url_pieces);
    $name_pre = $url_pieces[($url_pieces_count - 3)] . "-" . $url_pieces[($url_pieces_count - 2)] . "-";
    $name_final = ucwords($name_pre, "-");
    $zip = new ZipArchive;
    $download = $name_final . 'FileName.zip';
    $zip->open($download, ZipArchive::CREATE);
    foreach (glob("*.jpg") as $file) { 
       $zip->addFile($file);
    }
    $zip->close();
    header('Content-Type: application/zip');
    header("Content-Disposition: attachment; filename = $download");
    header('Content-Length: ' . filesize($download));
    header("Location: $download");
    ?>
    

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 2021-09-19
      • 2017-08-16
      • 1970-01-01
      • 2021-11-02
      • 2021-06-29
      • 2011-12-21
      • 2020-07-25
      • 1970-01-01
      相关资源
      最近更新 更多