PHP readfile返回数据

时间:2014-04-22 14:54:02

标签: php wordpress

(关于堆栈的第一个问题,很抱歉错误和/或不准确......)

所以:我开发了一个Wordpress主题,我希望我的用户下载帖子的附件。所以我创建了一个Zip Archive并使用了readfile()。该函数通过链接调用并使用$ _GET ['zip']测试。

问题:readfile()返回(我的意思是显示)这样的二进制字符(这里我试图下载测试.png文件): PNGIHDRp r * pHYSs OiCCPPhotoshopICCprofilexڝSgTS = BK KoR 没有下载任何内容。

这是我的代码(readfile部分):

$zip = new ZipArchive();
if ($zip->open($file_path.$archive_file_name, ZIPARCHIVE::CREATE)!==TRUE) {
  exit("cannot open <$archive_file_name>\n");
}
foreach($file_names as $file){
  $zip->addFile($file,$file);
}
$zip->close();
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($file_path.$file).'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file_path.$file));
ob_clean();
flush();
readfile($file_path.'screenshot.png');

我不知道在哪里搜索,这是我第一次使用zip存档和readfile()......

谢谢你的曲目(我们这么说吗?)!

1 个答案:

答案 0 :(得分:0)

我不知道究竟是怎么回事,但它现在有效。我在函数的开头添加了一个ob_start()。感谢您的支持!

整个功能:

if (!function_exists('zipThoseFiles')) {
    ob_start();
    function zipThoseFiles($id){
        if (!function_exists('zipFilesDownload')) {
            function zipFilesDownload($file_names,$archive_file_name,$file_path){
                $zip = new ZipArchive();
                if ($zip->open($file_path.$archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
                  exit("cannot open <$archive_file_name>\n");
                }
                foreach($file_names as $file){
                  $zip->addFile($file,$file);
                }
                $zip->close();

                header('Content-Description: File Transfer');
                header('Content-Type: application/zip');
                header('Content-Disposition: attachment; filename="'.basename($file_path.$archive_file_name).'"');
                header('Content-Transfer-Encoding: binary');
                header('Expires: 0');
                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                header('Pragma: public');
                header('Content-Length: ' . filesize($file_path.$archive_file_name));
                ob_end_clean();
                flush();
                readfile($file_path.$archive_file_name);
                unlink($file_path.$archive_file_name);
                exit; 
            }
        }
        $fileNames = array();
        $args = array(
            'post_type' => 'attachment',
            'numberposts' => null,
            'post_status' => null,
            'post_parent' => $id
        );
        $attachments = get_posts($args);
        if ($attachments) {
            foreach ($attachments as $attachment) {
                $fileNames[] = get_attached_file( $attachment->ID );
            }
        }
        $zip_file_name = 'mediasArchive.zip';
        $file_path = dirname(__FILE__).'/';
        zipFilesDownload($fileNames,$zip_file_name,$file_path);
    }
}