1

echo用来将各种文件输出到浏览器,包括一些 10MB+ .swf 文件。我遇到的一个负面影响是,它似乎与内容中的一些 Flash 预加载器混淆了,例如,导致图像闪烁而不是显示稳定的进度条。我想知道是否有任何缓冲正在进行或我能做些什么来修复它。

对于一些背景信息,我必须间接提供内容的原因是它被设计为仅提供给授权用户,因此脚本首先检查用户是否具有适当的权限,然后readfile()echo. 也许有更好的方法可以做到这一点,我对想法持开放态度。

编辑

readfile()当然,echo这是一种简化的解释方式。它运行类似:

<?php
check_valid_user();
ob_start();
// set appropriate headers for content-type etc
set_headers($file);

readfile($file);

$contents = ob_get_contents();
ob_end_clean();
if (some_conditions()) {
    // does some extra work on html files (adds analytics tracker)
}

echo $contents;
exit;
?>
4

3 回答 3

1

我认为在这种情况下缓冲是多余的。你可能会这样做:

<?php
    check_valid_user();
    // set appropriate headers for content-type etc
    set_headers($file);

    if (some_conditions())
    {
        $contents = file_get_contents($file);
        // does some extra work on html files (adds analytics tracker)
        // Send contents. HTML is often quite compressible, so it is worthwhile
        // to turn on compression (see also ob_start('ob_gzhandler'))
        ini_set('zlib.output_compression', 1);
        die($contents);
    }
    // Here we're working with SWF files.
    // It may be worthwhile to turn off compression (SWF are already compressed,
    // and so are PDFs, JPEGs and so on.
    ini_set('zlib.output_compression', 0);
    // Some client buffering schemes need Content-Length (never understood why)
    Header('Content-Length: ' . filesize($file));
    // Readfile short-circuits input and output, so doesn't use much memory.
    readfile($file);
    // otherwise:
    /*
    // Turn off buffering, not really needed here
    while(ob_get_level())
        ob_end_clean();
    // We do the chunking, so flush always
    ob_implicit_flush(1);

    $fp = fopen($file, 'r');
    while(!feof($fp))
        print fread($fp, 4096); // Experiment with this buffer's size...
    fclose($fp);
?>
于 2012-07-10T17:38:19.543 回答
0

您可能会使用fopen / fread等与 echo 和flush串联。如果您还没有,您可能需要事先调用header来设置内容类型。此外,如果您使用输出缓冲,则需要调用ob_flush

通过这种方式,您可以读取较小的数据并立即回显它们,而无需在输出之前缓冲整个文件。

于 2012-07-10T17:23:04.783 回答
0

您可能想尝试设置 Content-Length 标头。

就像是

header('Content-Length: ' . filesize($file));
于 2012-07-10T17:24:32.413 回答