2

** 更新 - 我知道它应该可以工作。我将 iframe 代码修复为仅回显参数而不是整行。不知何故,这解决了它。现在我可以异步下载一个文件并在当前的php中继续处理**

我正在尝试处理标题和 readfile()。调用 readfile() 后似乎没有执行任何代码。我推测是因为“一个 HTTP 请求,一个文件”规则。

我读到了使用 iframe 来绕过这个限制的建议。我尝试将此函数放在一个单独的文件中(例如 writezip)并从另一个文件中调用它,例如:

<!-- this code works and allows for an invisible file transfer-->
<iframe src="<? echo 'writezip.php' ?>" height="0px" width="0px" frameBorder="0" id="getfileurl"> </iframe>

<!-- this code does not and will not run the iframe asynchronously -->
<!-- Remove this line for a working solution -->
<? echo '<iframe src="Untitled-1.php">' ?>

<? echo "File was sent to you"; ?>

但这似乎不起作用(我在它自己的文件中删除了函数声明 - 所以它会在文件加载时执行)。有什么建议么?您将如何显示“文件已发送”的消息 - 提前致谢

<?php
function writezip(){
    $fullfilename = "post.zip";
    $outfilename = "test.zip";
    $mode = "download";

    if (file_exists($fullfilename)){
        // Parse Info / Get Extension
        $fsize = filesize($fullfilename);
        $path_parts = pathinfo($fullfilename);
        $ext = strtolower($path_parts["extension"]);

        // Determine Content Type
        switch ($ext) {
            case "pdf": $ctype="application/pdf"; break;
            case "exe": $ctype="application/octet-stream"; break;
            case "zip": $ctype="application/zip"; break;
            case "doc": $ctype="application/msword"; break;
            case "xls": $ctype="application/vnd.ms-excel"; break;
            case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
            case "gif": $ctype="image/gif"; break;
            case "png": $ctype="image/png"; break;
            case "jpeg":
            case "jpg": $ctype="image/jpg"; break;

            default: $ctype="application/force-download";
        }

        header("Pragma: public"); // required
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private",false); // required for certain browsers
        header("Content-Type: $ctype");

        $outfilename = ($outfilename=="" ? basename($fullfilename) : $outfilename);
        if ($mode == "view"){
            // View file
            header('Content-Disposition: inline; filename='.$outfilename);
        } 
        else {
            // Download file
            header('Content-Disposition: attachment; filename='.basename($outfilename));
        }

        header("Content-Transfer-Encoding: binary");
        header("Content-Length: ".$fsize);

        if (ob_get_length() > 0 ) {
            ob_clean();
            flush();
        }
        readfile( $fullfilename );
        die("anything here");
    }
    else
    {
        echo "File not found: ". $fullfilename;
    }
}
?>
4

1 回答 1

0

看起来像我的原始代码:

<? echo '<iframe src="Untitled-1.php">' ?> 

由于某种原因没有工作。原作品中的更新代码。

于 2013-09-18T01:01:08.737 回答