第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

目錄中的 Zip 文件使用 URL 的一部分作為文件名

目錄中的 Zip 文件使用 URL 的一部分作為文件名

PHP
德瑪西亞99 2023-07-30 12:44:42
我使用 php 文件創(chuàng)建文件夾中所有 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 的一部分作為存檔名稱?我的網(wǎng)址如下所示:https://www.example.com/data/pictures/album/我希望存檔名稱為Pictures-Album-CustomText.zip
查看完整描述

2 回答

?
LEATH

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊

您有幾個(gè)選擇:


$_SERVER['PHP_SELF']選項(xiàng) 1:使用、substr()和的組合str_replace()。


$_SERVER['PHP_SELF']選項(xiàng) 2:使用、rtrim()、explode()和的組合count()。


第一個(gè)選項(xiàng),細(xì)分:


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

?>

第二個(gè)選項(xiàng),細(xì)分:


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

?>


查看完整回答
反對 回復(fù) 2023-07-30
?
元芳怎么了

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊

這成為我的最終代碼(@Mech 代碼 + ucwods),我使用 ucwords 來對單詞進(jìn)行功能化 -


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

?>


查看完整回答
反對 回復(fù) 2023-07-30
  • 2 回答
  • 0 關(guān)注
  • 153 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號