繁体   English   中英

使用ziparchive或其他php脚本允许用户从zip文件夹中下载所有文件

[英]Using ziparchive or other php script to allow user to download all files from a folder in zip

因此,id希望为我的用户提供从我的站点下载特定文件夹/目录中的所有文件的选项(我们称其为批量下载)。

id要做的是,当用户单击链接/按钮时,脚本将创建特定文件夹中所有文件的临时zip文件,并且用户可以下载该文件。 (如果需要的话,我需要在不同页面上使用链接/按钮的不同实例来从我选择的其他文件夹中下载文件)。 一段时间后,该zip文件将被删除。 在下载完成后说。 我认为ziparchive可以做到这一点,但我不知道从哪里开始或如何实现。 它是一个joomla网站,我找不到任何可以做到这一点的扩展。

我不了解有关php的第一件事,所以我希望有人愿意帮助我,如果可能的话。 谢谢

如果你的服务器允许从PHP shell命令的执行,和zip安装,您可以生成与飞拉链passthru( "zip - directory" ) -表示要写入stdout ,这使您不必处理临时文件清除。

以下是此类脚本的概述:

<?php
if ( ! $dir = get_my_directory() )
    die("Illegal call.");

header( 'Content-Type: application/zip' );
header( 'Content-Disposition: attachment; filename=\"your.zip\"' );
passthru( 'zip -r - ' . escapeshellarg( $dir ) );

/**
 * @return false/null or the directory to zip.
 */
function get_my_directory() {
    ....
    return ....;
}

但是,您实现get_my_directory() ,请确保任何人都不可能在您的服务器上指定任何路径!

另外,请勿生成任何输出(无echo / print或警告),因为这样将不会设置标头,或者会损坏zip二进制数据。

除此之外,PHP的ZipArchive页面上还有代码示例和文档。

更新

(@ OP:如果您不了解任何PHP,我不太确定您正在实施PHP解决方案。但是,让我们假设您想学习。)

假设您要提供3个公共目录供下载,任何人都可以下载它们。 您将实现如下:

function get_my_directory() {
    // list of the directories you want anyone to be able to download.
    // These are key-value pairs, so we can use the key in our URLs
    // without revealing the real directories.
    $my_directories = array(
       'dir1' => 'path/to/dir1/',
       'dir2' => 'path/to/dir2/',
       'dir3' => 'path/to/dir3/'
    );

    // check if the 'directory' HTTP GET parameter is given:
    if ( ! isset( $_GET['directory'] ) )
        return null;               // it's not set: return nothing
    else
        $dir = $_GET['directory']; // it's set: save it so we don't have
                                   // to type $_GET['directory'] all the time.

    // validate the directory: only pre-approved directories can be downloaded
    if ( ! in_array( $dir, array_keys( $my_directories ) ) )
       return null;                    // we don't know about this directory
    else
       return $my_directories[ $dir ]; // the directory: is 'safe'.
}

是的,您将第一个和第二个代码示例粘贴到一个.php文件中(请确保将第一个get_my_directory函数替换为第二个.php文件),该文件位于服务器上可以访问的位置。

如果您将文件命名为“ download-archive.php”,并将其放置在DocumentRoot中,则可以通过http://your-site/download-archive.php?directory=dir1等进行访问。

以下是一些参考:

更新2

这是使用ZipArchive的完整脚本。 它仅在目录中添加文件; 没有子目录。

<?php
if ( ! $dir = get_my_directory() )
    die("Illegal call.");

$zipfile = make_zip( $dir );
register_shutdown_function( function() use ($zipfile) {
    unlink( $zipfile ); // delete the temporary zip file
} );

header( "Content-Type: application/zip" );
header( "Content-Disposition: attachment; filename=\"$zipfile\"" );
readfile( $zipfile );

function make_zip( $dir )
{
    $zip = new ZipArchive();
    $zipname = 'tmp_'.basename( $dir ).'.zip';  // construct filename
    if ($zip->open($zipname, ZIPARCHIVE::CREATE) !== true)
        die("Could not create archive");


    // open directory and add files in the directory
    if ( !( $handle = opendir( $dir ) ) )
        die("Could not open directory");

    $dir = rtrim( $dir, '/' );      // strip trailing /
    while ($filename = readdir($handle)) 
        if ( is_file( $f = "$dir/$filename" ) )
            if ( ! $zip->addFile( $f, $filename ) )
                die("Error adding file $f to zip as $filename");

    closedir($handle);

    $zip->close();

    return $zipname;
}


/**
 * @return false/null or the directory to zip.
 */
function get_my_directory() {
    // list of the directories you want anyone to be able to download.
    // These are key-value pairs, so we can use the key in our URLs
    // without revealing the real directories.
    $my_directories = array(
       'dir1' => 'path/to/dir1/',
       'dir2' => 'path/to/dir2/',
       'dir3' => 'path/to/dir3/'
    );

    // check if the 'directory' HTTP GET parameter is given:
    if ( ! isset( $_GET['directory'] ) )
        return null;               // it's not set: return nothing
    else
        $dir = $_GET['directory']; // it's set: save it so we don't have
                                   // to type $_GET['directory'] all the time.

    // validate the directory: only pre-approved directories can be downloaded
    if ( ! in_array( $dir, array_keys( $my_directories ) ) )
       return null;                    // we don't know about this directory
    else
       return $my_directories[ $dir ]; // the directory: is 'safe'.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM