猿问

目录中的 Zip 文件使用 URL 的一部分作为文件名

我使用 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


德玛西亚99
浏览 117回答 2
2回答

LEATH

您有几个选择:$_SERVER['PHP_SELF']选项 1:使用、substr()和的组合str_replace()。$_SERVER['PHP_SELF']选项 2:使用、rtrim()、explode()和的组合count()。第一个选项,细分:$url = $_SERVER['PHP_SELF'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the current full urlstrrpos($url, "pictures/")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // finds "pictures/" in the $url variablesubstr($url, strrpos($url, "pictures/"))&nbsp; // extracts everything from "pictures/" onwardsstr_replace("/","-", $name_pre);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // replaces "/" with "-"&nbsp;<?php&nbsp; &nbsp; $url = $_SERVER['PHP_SELF'];&nbsp; &nbsp; $name_pre = substr($url, strrpos($url, "pictures/"));&nbsp; &nbsp; $name_pre = str_replace("/","-", $name_pre);&nbsp; &nbsp; $zip = new ZipArchive;&nbsp; &nbsp; $download = $name_pre . 'FileName.zip';&nbsp; &nbsp; $zip->open($download, ZipArchive::CREATE);&nbsp; &nbsp; foreach (glob("*.jpg") as $file) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;$zip->addFile($file);&nbsp; &nbsp; }&nbsp; &nbsp; $zip->close();&nbsp; &nbsp; header('Content-Type: application/zip');&nbsp; &nbsp; header("Content-Disposition: attachment; filename = $download");&nbsp; &nbsp; header('Content-Length: ' . filesize($download));&nbsp; &nbsp; header("Location: $download");?>第二个选项,细分:$url = rtrim($_SERVER['PHP_SELF'], "/"); // get url and remove trailing "/"$url_pieces = explode('/', $url);&nbsp; &nbsp; &nbsp; &nbsp; // break string into pieces based on "/"$url_pieces_count = count($url_pieces);&nbsp; // count the number of pieces$name_pre = $url_pieces[($url_pieces_count - 2)] . "-" . $url_pieces[($url_pieces_count - 1)] . "-"; // construct the filename preface<?php&nbsp; &nbsp; $url = rtrim("https://www.example.com/data/pictures/album/", "/");&nbsp; &nbsp; $url_pieces = explode('/', $url);&nbsp; &nbsp; $url_pieces_count = count($url_pieces);&nbsp; &nbsp; $name_pre = $url_pieces[($url_pieces_count - 2)] . "-" . $url_pieces[($url_pieces_count - 1)] . "-";&nbsp; &nbsp; $zip = new ZipArchive;&nbsp; &nbsp; $download = $name_pre . 'FileName.zip';&nbsp; &nbsp; $zip->open($download, ZipArchive::CREATE);&nbsp; &nbsp; foreach (glob("*.jpg") as $file) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;$zip->addFile($file);&nbsp; &nbsp; }&nbsp; &nbsp; $zip->close();&nbsp; &nbsp; header('Content-Type: application/zip');&nbsp; &nbsp; header("Content-Disposition: attachment; filename = $download");&nbsp; &nbsp; header('Content-Length: ' . filesize($download));&nbsp; &nbsp; header("Location: $download");?>

元芳怎么了

这成为我的最终代码(@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) {&nbsp;&nbsp; &nbsp;$zip->addFile($file);}$zip->close();header('Content-Type: application/zip');header("Content-Disposition: attachment; filename = $download");header('Content-Length: ' . filesize($download));header("Location: $download");?>
随时随地看视频慕课网APP
我要回答