不要从数组中删除重复的值

时间:2019-01-31 10:02:54

标签: php download zip zipfile ziparchive

将这些文件添加到zip文件中后,我编写了此代码来下载文件。但是,我的代码删除了具有相同名称(重复)的文件。

<?php
# define file array
$files = array(
        'https://www.fbise.edu.pk/Old%20Question%20Paper/2017/SSC-II/Chemistry.PDF', 
        'https://www.fbise.edu.pk/Old%20Question%20Paper/2018/SSC-II/Chemistry.PDF', 
        'https://www.fbise.edu.pk/Old%20Question%20Paper/2018/SSC-II/Physics.PDF', 
        'https://www.fbise.edu.pk/Old%20Question%20Paper/2017/SSC-II/Physics.PDF',
);

# create new zip object
$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="my file.zip"');
header('Content-type: application/zip');
readfile($tmp_file);
unlink($tmp_file);
?>

所以我的问题是

  • 如何从阵列下载重复文件?

  • 如何更改重复文件的名称?

1 个答案:

答案 0 :(得分:0)

只需重命名文件即可。

<?php
# define file array
$files = array(
        'https://www.fbise.edu.pk/Old%20Question%20Paper/2017/SSC-II/Chemistry.PDF', 
        'https://www.fbise.edu.pk/Old%20Question%20Paper/2018/SSC-II/Chemistry.PDF', 
        'https://www.fbise.edu.pk/Old%20Question%20Paper/2018/SSC-II/Physics.PDF', 
        'https://www.fbise.edu.pk/Old%20Question%20Paper/2017/SSC-II/Physics.PDF',
);

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

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

// Variable to Keep Filenames
$filename = '';
// Variable to add "-1, -2" to duplicate files
$i = 1;

# loop through each file
foreach ($files as $file) {
    # download file
    $download_file = file_get_contents($file);

    if ( $filename == basename($file) )
    {
        // If this file already exists add "-1, -2"
        $filename = $i . '-' . basename($file);
        $i++;
    } else 
    {
        $filename = basename($file);
        $i = 1;
    }

    #add it to the zip
    $zip->addFromString($filename, $download_file);
}

# close zip
$zip->close();

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

在@RiggsFolly注释后编辑,以支持乱序重复文件

<?php
# define file array
$files = array(
        'https://www.fbise.edu.pk/Old%20Question%20Paper/2017/SSC-II/Chemistry.PDF', 
        'https://www.fbise.edu.pk/Old%20Question%20Paper/2018/SSC-II/Chemistry.PDF', 
        'https://www.fbise.edu.pk/Old%20Question%20Paper/2018/SSC-II/Physics.PDF', 
        'https://www.fbise.edu.pk/Old%20Question%20Paper/2017/SSC-II/Physics.PDF',
);

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

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

// Array to keep filenames
$filenames = array();

# loop through each file
foreach ($files as $file) {
    # download file
    $download_file = file_get_contents($file);

    if( array_key_exists( basename($file), $filenames ) )
    {
        $filename =  $filenames[basename($file)] . '-' . basename($file);
        $filenames[basename($file)] = $filenames[basename($file)] + 1;
    } else
    {
        $filename = basename($file);
        $filenames[basename($file)] = 1;
    }

    #add it to the zip
    $zip->addFromString($filename, $download_file);
}

# close zip
$zip->close();

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