/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] when downloading a file, I can’t navigate to other pages

Hi All,

I’m using a download script to download files from my site just so I don’t show the file path and don’t allow direct downloads.

Here’s the part that does the download after the database and logging stuff are taken care of:

[CODE]$speed = 500;

$fsize = filesize($fullPath);
header(“Pragma: public”);
header(“Expires: 0”);
header(“Cache-Control: must-revalidate, post-check=0, pre-check=0”);
header(“Cache-Control: public”);
header(“Content-Description: File Transfer”);
//header(“Content-Type: “.$mime_type);
header(“Content-Disposition: attachment; filename=””.$row[“file_name”].”””);
header(“Content-Transfer-Encoding: binary”);
header(“Content-Length: ” . $fsize);

$file = @fopen($fullPath,”rb”);
if ($file) {
while(!feof($file)) {
echo fread($file, round(1024*$speed));
flush();
sleep(1);
if (connection_status()!=0) {
@fclose($file);
die();
}
}
@fclose($file);
}[/CODE]

Now I have two questions/problems:

1) my speed is set to 500, doesn’t that mean max 500KB/sec? why am I only able to download at around 250KB/sec? If I remove the speed limits, then I can download up to 1.5MB/sec.

2) The main issues with this download script is while a file is being downloaded, I can’t navigate to any other page on my site. even if I have 3 or 4 tabs/windows open, none of the links within my site work anymore in any of the other windows until the download is completed.

I can however, navigate around the internet in other sites, just not my own site.

I’m pretty sure the issue is my download script because if I allow direct downloads, then I can navigate within the site while the file is being downloaded.

Thank a lot in advance for all your help.

SIA

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@SrWebDeveloperDec 07.2009 — The speed value in your situation is not bandwidth, rather how many bytes are being read from disk into fread's file pointer. Setting a value of 8192 is common for files of varying size on a typical server. Bandwidth is controlled by the HTTP protocol layer, although fread will stop reading when a new packet is available during a stream according to the docs. PHP inteprets one line at a time so a page will be "busy" if you set the headers to force a download until the procedure (streaming) is completed.

In your case the solution might be as simple as opening the download in a new window, i.e. setting target in a link or form tag to _blank or whatever.

-jim
Copy linkTweet thisAlerts:
@siavash1979authorDec 07.2009 — Thanks for your reply Jim.

But that didn't help. I made it a link and I right clicked on it and selected "open in a new window". The download started in a new window and still none of the other pages I had open let me navigate anywhere.

The weird thing is that I have PHPMyAdmin installed on the web server, and I can easily go to that and navigate within it. So just my own site won't let me click on anything else until the download is over, but the server can handle other requests.

Is there an apache setting that can limit users to just one instance of the site at a time?

Here's a link that you can use for testing: http://www.spidersend.com/download.php?getfile=1&code=74ba7fa4-7d5b-4ad9-b83a-3eef73e7c6df&filename=Pro_Drupal_Development_Second_Edition.pdf

While that file is being downloaded, you can't click on anything else on that same page, or even if you open up a link in a new tab/window. Everything waits until the download is finished.

Thanks again for any help in advance.

Sia
Copy linkTweet thisAlerts:
@siavash1979authorDec 07.2009 — Sorry, I put the wrong link in the post above, and it won't let me edit my post. Here's the right link which takes you to the download page first and then starts the download:

http://www.spidersend.com/download.php?code=74ba7fa4-7d5b-4ad9-b83a-3eef73e7c6df&filename=Pro_Drupal_Development_Second_Edition.pdf

Thanks again,

Sia
Copy linkTweet thisAlerts:
@SrWebDeveloperDec 07.2009 — Before looking at the web server setup, let's rule out the scripting. I'm thinking do you use session control which occurs prior to the download page being initiated? Or if you use include/require to load the download page maybe check the sequence or change to include_once. Start looking at stuff like that. If no session conflict and no issues with include/require then the next thing I want to see is your entire phpinfo output.

-jim
Copy linkTweet thisAlerts:
@siavash1979authorDec 07.2009 — Thank you very much Jim.

You're right, the problem seems to be the session_start() command that I have in my config.inc.php file. If I kill the session right before the download starts, then the download is fine and I can navigate to other pages. But I don't really want to kill the session so if a user is logged in, I'd like him/her to stay logged in.

Can you think of any other way around this?

Thanks a lot,
--


Sia

http://www.siawebdesign.com

http://www.spidersend.com
Copy linkTweet thisAlerts:
@SrWebDeveloperDec 08.2009 — Your issue is recursive sessions usage which means your download script at the top calls session_start(), but config.inc.php already started the session. The solution is to simply replace the session_start in your download page with "include config.inc.php"; using the proper path of course. That's my best guess. ?

-jim
Copy linkTweet thisAlerts:
@siavash1979authorDec 08.2009 — Thanks Jim,

actually I don't have a session_start() in any of my pages. all my pages only have require_once("./config.inc.php"); on top and the config.inc.php is the only file that calls session_start in it.

Now I can't really kill the session because that would log out the user. I think I'll just put a check to see if I'm in the download page, and if I am, it'll skip session_start in my config file.

Thanks again for all your help.


__________________
Thanks,

SIA Web Design

Got a File to send? ... Spider Send it
Copy linkTweet thisAlerts:
@siavash1979authorDec 08.2009 — I found a solution ?

All I had to call was session_write_close(); instead of session_destroy();

That keeps my session open and solves the problem with navigating to other pages too.
Copy linkTweet thisAlerts:
@SrWebDeveloperDec 08.2009 — Glad I focused you in the right direction and you were able to resolve on your own. Sessions can be a bit tricky when initiated recursively. Cheers!

-jim
×

Success!

Help @siavash1979 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...