Serving Downloads without readfile or mod_xsendfile ?

Ask about a PHP problem here.
Post Reply
alionsonny
Posts: 1
Joined: Wed Apr 04, 2012 6:37 am

Serving Downloads without readfile or mod_xsendfile ?

Post by alionsonny »

Hello!
I have quite a webprogramming problem I addressed at various places on the web, but there seems to be no real solution. So I try it here and maybe somebody can help me.

What I want to do:
I want to build a webshop for my own music. It should be buyable in form of single MP§ files as well as RAR archives for complete albums. Once the customer has payed for the items, he should find download links in the "My Downloads" section of his account. He should be able to redownload the files as many times as he wishes. For obvious reasons, I don't want to give direct links to the files ;)

The situation:
My hoster has a timelimit for PHP scripts of 2 minutes. This may be fine for single MP3s on a good internet connection. But if one has a slower internet connection or the download is a RAR album with about 95 MB size, the download will break after 2 minutes. A solution for this problem would be to use the Apache module mod_xsendfile, but the hoster refuses to activate it for my shared hosting package.

Question:
Does anybody have an idea how to still solve that problem? Please don't tell me stuff like "Switch the hoster", because I have just renewed the contract term. Yeah, I didn't know about those shortcomings of the hosting package. So is there a way to get the stuff I want done?

Thanks very much for any help and excuse my lousy english, I'm german
Sonny
User avatar
KnightMaire
Posts: 29
Joined: Thu May 05, 2011 9:03 pm

Re: Serving Downloads without readfile or mod_xsendfile ?

Post by KnightMaire »

If you are able to edit your PHP.ini file by using the ini_set() function, you can use:
ini_set('max_execution_time', [number of seconds]);
or even easier:
set_time_limit( [number of seconds] );
Both modify PHP.ini temporarily.

If your host controls the execution time of Apache, that's a different story and I am clueless as to how it would be bypassed.

Hope this helps.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Serving Downloads without readfile or mod_xsendfile ?

Post by jacek »

Well assuming getting the time limit removed is not possible you will have to resort to a very hacky solution, basically you will need to catch the script time out and just give up on speed limiting and send the whole file in the last second if you have to.
	$timer_begin = microtime(true);
	
	while (!feof($file)){
		echo fread($file, 256);
		
		if (microtime(true) - $timer_begin < 58){
			usleep($delay);
		}
	}
There may also be a way to redirect the browser to a new location for the rest of the file, but that is not very likely to work.

If you are not doing speed limiting at all then there is nothing to worry about since the timeout only applies to the time the script is running and it will exit once it has sent all of the data to Apache which can be before the download has finished.
Image
Post Reply