homepage Welcome to WebmasterWorld Guest from 184.72.82.126
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
File Download stops Browsing
LooInSpain




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

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




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

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




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

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




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


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




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

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




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved