在 php 中创建 zip 文件并在添加之前重命名每个文件 [英] create zip file in php and rename each file before add

查看:29
本文介绍了在 php 中创建 zip 文件并在添加之前重命名每个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码来创建 mp3 的 zip.我能够成功地使用它创建 zip.但是如何在制作 zip 之前重命名每个文件...

i am using below code to create zip of mp3s. i am able to create zip using this successfully. but how can i rename each file before making zip...

    # define file array
    $files = array(
      'http://google.com/images/logo.png',
      'http://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Wikipedia-logo-en-              big.png/220px-Wikipedia-logo-en-big.png'
     );

     # create new zip opbject
     $zip = new ZipArchive();

     # create a temp file & open it
     $tmp_file = tempnam('.','');
     $zip->open($tmp_file, ZipArchive::CREATE);

     # loop through each file
     foreach($files as $file){

        # download file
        $download_file = file_get_contents($file);

        #add it to the zip
        $zip->addFromString(basename($file),$download_file);

    }

    # close zip
    $zip->close();

    # send the file to the browser as a download
    header('Content-disposition: attachment; filename=download.zip');
    header('Content-type: application/zip');
    readfile($tmp_file);

推荐答案

您可以使用 renameName 在 addFromString 方法行之后:

You can use renameName after the addFromString method line:

$zip->addFromString(basename($file), $download_file);
$zip->renameName(basename($file), 'nicename.mp3');

这篇关于在 php 中创建 zip 文件并在添加之前重命名每个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆