2 回答
TA贡献1936条经验 获得超6个赞
您有几个选择:
$_SERVER['PHP_SELF']选项 1:使用、substr()和的组合str_replace()。
$_SERVER['PHP_SELF']选项 2:使用、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");
?>
TA贡献1798条经验 获得超7个赞
这成为我的最终代码(@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");
?>
- 2 回答
- 0 关注
- 116 浏览
添加回答
举报