Zip-файлы в каталоге используют часть URL-адреса в качестве имени файла - PullRequest
1 голос
/ 12 июля 2020

Я использую файл php, чтобы создать zip-архив всех файлов jpg в папке и сделать его доступным для загрузки.

Вот сценарий:

<?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-адреса в качестве имени архива? Мои URL-адреса выглядят так:

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

Я хочу, чтобы имя архива было Pictures-Album-CustomText.zip

Ответы [ 2 ]

1 голос
/ 13 июля 2020

Это становится моим окончательным кодом (@Mech code + 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");
?>
1 голос
/ 12 июля 2020

У вас есть несколько вариантов:

Вариант 1: Использование комбинации $_SERVER['PHP_SELF'], substr() и str_replace().

Вариант 2: Использование комбинации $_SERVER['PHP_SELF'], rtrim(), explode() и count().

1-й вариант в разбивке:

$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");
?>

2-й вариант, в разбивке:

$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");
?>
...