【问题标题】:Download random files as zip将随机文件下载为 zip
【发布时间】:2016-03-07 17:43:31
【问题描述】:

几天前我发布了我的代码,现在我已经掌握了它。然而,我已经获得了随机文件,当它们压缩时,解压缩后它变成了一个 zip.cpgz 文件。我确信这与我在循环中使用数组的方式有关,但我不太确定如何解决这个问题。

 <?php 
 //./uploads
$dir = "./uploads";
$nfiles = glob($dir.'*.{aiff}', GLOB_BRACE);     
$n=1;
while ($n<=10){
$n ++;
$arr[$n] = rand(2, sizeof($nfiles)-1);
print($arr[$n]);
print("   ");
}


$zip = new ZipArchive();
$zip_name = "zipfile.zip";
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){
    $error .= "* Sorry ZIP creation failed at this time";
}

foreach($arr as $file){
    $path = "./uploads".$file;
    $zip->addFromString(basename($path),  file_get_contents($path)); 
}

$zip->close();

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zip_name);
readfile('zipfile.zip');

?>

另外,如果您在这里有点迷路,my website 我正在尝试实现它。 (点击下载按钮)

【问题讨论】:

  • 当我尝试下载链接时,我得到了一个包含 21 个空文件的 zip 存档!
  • 顺便说一句 - 您网站上的 html 无效 - upload 链接、&lt;p class="mbr-footer__copyright"&lt; &lt;/p&gt;&lt;p&gt;Follow me! 没有关闭 a 标记,然后底部有一个虚假的额外 body 标记对...

标签: php html random directory


【解决方案1】:

最近帮助另一个用户获得了与工作类似的东西(没有随机选择),您可能会发现以下内容很有用。这确实会搜索特定文件扩展名的目录,然后随机选择 10 个压缩并发送的文件。更改 $sourcedir$ext 以适应 - 希望对您有所帮助。

/* From David Walsh's site - modified */
function create_zip( $files = array(), $destination = '', $overwrite = false ) {
    if( file_exists( $destination) && !$overwrite ) { return false; }
    $valid_files = array();
    if( is_array( $files ) ) {
        foreach( $files as $file ) if( file_exists( $file ) ) $valid_files[] = $file;
    }
    if( count( $valid_files ) ) {
        $zip = new ZipArchive();
        if( $zip->open( $destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE ) !== true) return false;
        foreach( $valid_files as $file ) $zip->addFile( $file, pathinfo( $file, PATHINFO_FILENAME ) );
        $zip->close();
        return file_exists( $destination );
    }
    return false;
}
/* Simple function to send a file */
function sendfile( $filename=NULL, $filepath=NULL ){
    if( file_exists( $filepath ) ){

        if( !is_file( $filepath ) or connection_status()!=0 ) return FALSE;

        header("Cache-Control: no-store, no-cache, must-revalidate");
        header("Pragma: no-cache");
        header("Expires: ".gmdate("D, d M Y H:i:s", mktime( date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
        header("Content-Type: application/octet-stream");
        header("Content-Length: ".(string)( filesize( $filepath ) ) );
        header("Content-Disposition: inline; filename={$filename}");
        header("Content-Transfer-Encoding: binary\n");

        if( $file = @fopen( $filepath, 'rb' ) ) {
            while( !@feof( $file ) and ( connection_status()==0 ) ) {
                print( fread( $file, 1024*8 ) );
                flush();
            }
            @fclose( $file );
        }
        return( ( connection_status()==0 ) and !connection_aborted() );
    }
}



/* Select a random entry from the array */
function pick( $arr ){
    return $arr[ rand( 0, count( $arr )-1 ) ];
}

/* The directory to which the zip file will be written before sending */
$target=__DIR__.'\zipfile.zip';

/* The directory you wish to scan for files or create an array in some other manner */
$sourcedir = 'C:\Temp\temp_uploads';

/* File extension to scan for */
$ext='txt';

/* Placeholder to store files*/
$output=array();

/* Scan the dir, or as mentioned, create an array of files some other way */
$files=glob( realpath( $sourcedir ) . DIRECTORY_SEPARATOR . '*.'.$ext );


/* Pick 10 random files from all possible files */
do{
    $rnd=pick( $files );
    $output[ $rnd ] = $rnd;
}while( count( $output ) < 10 );

/* streamline array */
$output=array_values($output);



if( $target ) {
    /* Zip the contents */
    $result=create_zip( $output, $target, true );

    /* Send the file - zipped! */
    if( $result ) {
        $res=call_user_func( 'sendfile', 'zipfile.zip', $target );
        if( $res ) unlink( $target );
    }
}

【讨论】:

  • 你就是男人!哇,这样做更有意义,显然我的电脑是唯一不让我解压缩的电脑……它对你有用吗?
  • 我可以下载 zip 文件,但所有 21 个文件都是空的,现在我收到了 Fatal error: Maximum execution time of 30 seconds exceeded in /home/ghostx19/public_html/phDownloader.php on line 47
  • 我应该如何解决这个错误..我很抱歉我还在学习
  • 压缩文件仍然包含空文件,但速度问题似乎已经解决,现在关于最大执行时间没有错误。您还面临问题吗?
  • 我只是不确定为什么文件是空的。数组只是保存名称还是什么?
【解决方案2】:

你确定它不起作用?我已经下载了你的 zip 文件并解压了它,我得到了 UploadsX 文件。所以我没有得到一个 zip.cpgz 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多