Welcome to WebmasterWorld Guest from 54.144.79.200

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

File Download stops Browsing

     

LooInSpain

5:38 pm on Oct 23, 2009 (gmt 0)

5+ Year Member



I've written a download script to act as a proxy for files on my website so people can't get a direct link. The script works perfect, except that when you try to continue browsing after the file download has started, you can't!

A direct link to the file causes no such problem, you can continue to browse through the site, but when downloading through the download.php file, you can't - it just times out.

Here's my script:


<?php
# Declare function
function send_download($filename,$phone_model){
$file_path = "software/$phone_model/$filename";
$filetype = strrchr($filename,'.');
$ext = strtolower($filetype);
$ext = str_replace(".", "", $ext);
switch($ext) {
case "zip":
$ext = 'application/zip';
break;
case "exe":
$ext = 'application/octet-stream';
break;
case "dmg":
$ext = 'application/x-apple-diskimage';
break;
}

$file_size = filesize($file_path);

// I even tried to limit the d/l rate which didn't help
$download_rate = 1.5;

header("Content-Description: File Transfer");
header("Content-Type: $ext");
header("Content-disposition: attachment; filename=$filename");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $file_size");
//readfile($file_path);
$file = @fopen($file_path,"rb");
if ($file) {
while(!feof($file)) {
set_time_limit(0);
print fread($file, round($download_rate * 1024));
flush();
sleep(1);
if (connection_status()!=0) {
@fclose($file);
die();
}
}
@fclose($file);
}
}
if (isset($_GET['file'])){
$file = $_GET['file'];
$member_query = mysql_query("SELECT * FROM members WHERE email_address = '$memberID'") or die (mysql_error());
while ($r = mysql_fetch_array($member_query)) {
$phone_model = $r["phone_model"];
}

if (file_exists("software/$phone_model/$file")) {
send_download($file, $phone_model);
} else {
echo "You Are Not Allowed to Download This Content";
}
} else {
echo "Error";
}
?>


This page opens in a blank window and sends the file. Can anyone see why it would slow down/stop the browsing in the parent window? Please help! This is not a browser specific bug either - tested in IE, Firefox, Chrome and Mac based Safari - all with the same results.

eelixduppy

10:22 pm on Nov 9, 2009 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



No clue. Have you resolved this issue yet? It seems like the browser that is the one to blame here, and not your script (which I looked over quickly and didn't see anything that stood out). But perhaps that is the intended behavior of the browsers. Have you experimented and taken it out of a popup window and tested it like that?

LooInSpain

10:41 pm on Nov 9, 2009 (gmt 0)

5+ Year Member



Hi eelixduppy, thanks for the reply.

No, I haven't sorted this out yet. I took the download out of a popup window and tried in a target "_blank" which made no difference and then made it a direct link. Again, this didn't change anything, the browsing after continued to be not just slow, but unable to display the next page on the site until the download was stopped. I tried in four different web browsers to check it wasn't Firefox specific.

One thing I haven't posted in the code is that there is a PHP Session Control to prevent non-members accessing the downloads, would this be causing the problem?

JohnCanyon

1:34 am on Nov 10, 2009 (gmt 0)

10+ Year Member




One thing I haven't posted in the code is that there is a PHP Session Control to prevent non-members accessing the downloads, would this be causing the problem?

That is exactly your problem. The session is locking the connection, look at session_write_close() before you send the download stream.

That should fix your issue.

//add here
session_write_close();

header("Content-Description: File Transfer");
header("Content-Type: $ext");
header("Content-disposition: attachment; filename=$filename");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $file_size");
//readfile($file_path);
$file = @fopen($file_path,"rb");
if ($file) {
while(!feof($file)) {
set_time_limit(0);
print fread($file, round($download_rate * 1024));
flush();
sleep(1);
if (connection_status()!=0) {
@fclose($file);
die();
}
}
@fclose($file);
}
}

Cheers.

LooInSpain

4:29 pm on Nov 12, 2009 (gmt 0)

5+ Year Member



that was the problem there i didnt close the session write

Thanks for your help I couldn't see the wood for the trees

LooInSpain

4:37 pm on Nov 12, 2009 (gmt 0)

5+ Year Member



The only change I had to make was the location of the write close, I had to put it before the file was sent, but after all of the member id info that is stored in the session variables as this was not registering into the database before the file was sent.

Thanks again

 

Featured Threads

Hot Threads This Week

Hot Threads This Month