Forum Moderators: coopster

Message Too Old, No Replies

recording completed downloads to a database problem

method may be causing sporadically high CPU usage, is there a better way?

         

jezra

7:00 pm on Jan 28, 2005 (gmt 0)

10+ Year Member



I needed to record completed downloads to a database. The method I used is a follows

$filePath = the_path_to_a_file;
$file = fopen($filePath,"rb");
// send custom headers
header("Content-type: application/octet-stream");
header("Content-Disposition: inline; filename=\"$fileName\"");
header("Content-length: ".(string)(filesize($filePath)));
while(!feof($file) )// if we haven't got to the End Of File
{
print(fread($file, 1024*8) );//read 8k from the file and send to the user
flush();//force the previous line to send its info
if (connection_status()!=0)//check the connection, if it has ended...
{
fclose($file);//close the file
die();//kill the script
}
}
fclose($file);//close the file
//update the request count in the db
code to update the database goes here

because a user may be downloading a file larger than 30Meg on a 56k modem, this script has a 10 hour time limit. I'm not positive that this script is causing my high CPU usage, however, my CPU problem started when I introduced this script to my site. I could change the script to record requested files, but I prefer to record dowloads that have completed. Does anyone know of a better, less server intensive way of recording completed downloads?

jatar_k

7:55 pm on Feb 1, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the problem is definitely this script it sounds like

if you get a bunch of users downloading at the same time your cpu usage will eventually get used up. Personally, I wouldn't record the completed download, just that it was initiated and then send them the file. Thatw ould allow the download but not keep the script itself running.

Any reason why you specifically need to track completed?

jezra

8:35 pm on Feb 1, 2005 (gmt 0)

10+ Year Member



The script is part of a upload/download system. When it was originally designed, there was talk of giving free software to the creator of the most downloaded file and I wanted to get a more accurate count than just the download requests. I also wanted users to be able to keep track of their completed downloads.