开发者

PHP download method hanging up page... best practices for pushing a download?

quick question: I've got a form that forces a user to enter an email, after which a download/attachment is pushed and a file is downloaded... the file downloads fine... however...

My problem is that when the download starts, the page locks up, and the user can't navigate anywhere or do anything on the page until the file is downloaded (ie: clicking the "go home" link below). Any better solutions than what I come up with here? I know i'm probably missing something really simple ... this is my first crack at setting up a private download page.

<script t开发者_开发技巧ype="text/javascript">
function redirect_function(loc){
    window.location = loc;
}
</script>

<?php
// after form is submitted
$condition_met=check($_POST['email']);

if($condition_met) { ?>
    <p>Your file will begin downloading in 5 seconds.</p> <a>go home</a>
    <script type="text/javascript">
        setTimeout('redirect_function("download.php")', 5000);
    </script>
<?php } ?>

The called (download.php) page looks like this, this is where it hangs up the page...

<?php
ob_start();
if($some_condition) { // check for authorization, etc
    $file='location/file.ext';
    header('Content-type: application/force-download');
    header('Content-disposition: attachment; filename="'. basename($file) .'"');
    header('Content-length: '. filesize($file) );
    readfile( $file );
} else {
    echo "error message";
}
ob_end_flush();
?>


Take the output buffer out. Readfile() will incrementally pop out the data as it goes, but your output buffer is catching it all until it gets to the flush.


almost there, you need to change the content type to

application/force-download
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜