views:

42

answers:

2

Hi there, 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 type="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();
?>
+1  A: 

almost there, you need to change the content type to

application/force-download
buggedcom
the file already downloads even with application/x-file-to-save, the issues is that when redirect_function() calls the download page, the page hangs up until the file finishes downloading... disallowing the user to click the "go home" link :( Thanks for the response!
Jon
Is this IE you are talking about? I think the only way around this would be to open the url in a new window?
buggedcom
actually. have you tried removing the output buffering?
buggedcom
+1  A: 

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.

UltimateBrent