random_qwerty Posted May 19, 2009 Share Posted May 19, 2009 Hi, I have been hitting my head against a wall for ages. I am trying to make a script for downloading files. It was working, until i guess i changed something(url decoding the filename), and I can't see how i can fix the script. The Problem: Although it can see the file, and it opens it etc, it will break the download at exactly 4,096 bytes. >_< Any thoughts? If you need any clarifications on the code, yes I can see it is quite messy and i don't comment much, I will begrudgingly try and help make it clearer for you. and also, what are your thoughts on my use of include to handle mysql db connections? $ep_number = $_GET[f]; //relates to a db entry for episode number $series = $_GET[z]; //relates to a db entry for series number $user = $_SESSION['MM_Username']; include('Connections/db_vars.php'); include('Connections/db_con.php'); $dl_details = mysql_query("SELECT * FROM downloads WHERE file_id = $ep_number && series_id = $series"); $dl_details = mysql_fetch_assoc($dl_details); mysql_close($con); $last_dl_date = date('H:i') . ' on ' . date('d-m-Y'); $last_dl = $dl_details['file_name']; $last_dl_size = $dl_details['size']; $dl_size = $dl_details['size']; $dl_directory = urldecode($dl_details['directory']); $file_dl = urldecode($dl_details['file_name']); header("Cache-Control: no-cache"); header("Expires: -1"); header("Content-disposition: attachment; filename=\"$file_dl\""); header("Content-type: application/octet-stream"); header("Content-length: $dl_size"); // set the download rate limit (=> 100 = 100 kb/s) $download_rate = 200; if(file_exists($dl_directory) && is_file($dl_directory)) { // flush content flush(); // open file stream $file_op = fopen($dl_directory, "rb"); while(!feof($file_op)) { // send the current file part to the browser print fread($file_op, round($download_rate * 1024)); // flush the content to the browser set_time_limit(0); ob_flush(); flush(); // sleep one second sleep(1); } // close file stream fclose($file_op); include('Connections/db_vars.php'); include('Connections/db_con.php'); mysql_query("UPDATE users SET LAST_DL = '$last_dl' , LAST_DL_DATE = '$last_dl_date' , TOTAL_AMOUNT_DL = TOTAL_AMOUNT_DL + '$last_dl_size' WHERE USERNAME = '$user'"); mysql_close($con); exit; } else { die('Error: The file '.$file.' does not exist!'); } Quote Link to comment https://forums.phpfreaks.com/topic/158672-download-script/ Share on other sites More sharing options...
MadTechie Posted May 19, 2009 Share Posted May 19, 2009 3 things to look at #1 check the files on the server #2 is $dl_size correct ? #3 is the the size 4,096 bytes. that fails or the time it takes to get to 4,096 bytes ? as this could well be a a timeout issue! try adding set_time_limit(0); Quote Link to comment https://forums.phpfreaks.com/topic/158672-download-script/#findComment-836891 Share on other sites More sharing options...
random_qwerty Posted May 19, 2009 Author Share Posted May 19, 2009 3 things to look at #1 check the files on the server #2 is $dl_size correct ? #3 is the the size 4,096 bytes. that fails or the time it takes to get to 4,096 bytes ? as this could well be a a timeout issue! try adding set_time_limit(0); 1. that was one of the first steps before i came here 2. that was one of the second steps before i came here 3. the size is always 4,096 bytes, (unless it was smaller than that ofcourse) I don't understand how a file size can equal time ... so I cant answer the second part of that 4. another thing i did before coming. see line 88 MOAR help please Quote Link to comment https://forums.phpfreaks.com/topic/158672-download-script/#findComment-836949 Share on other sites More sharing options...
DarkSuperHero Posted May 19, 2009 Share Posted May 19, 2009 Question for MODs/Guru's/Pro's Is there such a thing a a memory limit on this? would memory limit even apply to what OP is doing in his script? Quote Link to comment https://forums.phpfreaks.com/topic/158672-download-script/#findComment-836991 Share on other sites More sharing options...
Masna Posted May 19, 2009 Share Posted May 19, 2009 Question for MODs/Guru's/Pro's Is there such a thing a a memory limit on this? would memory limit even apply to what OP is doing in his script? Well, there's a memory limit on everything: especially when dealing with digital information. Most browsers probably obtain that limit from the header sent here: header("Content-length: $dl_size"); As for whether or not this is the issue in this particular case: I don't know. Quote Link to comment https://forums.phpfreaks.com/topic/158672-download-script/#findComment-836997 Share on other sites More sharing options...
random_qwerty Posted May 19, 2009 Author Share Posted May 19, 2009 Question for MODs/Guru's/Pro's Is there such a thing a a memory limit on this? would memory limit even apply to what OP is doing in his script? Well, there's a memory limit on everything: especially when dealing with digital information. Most browsers probably obtain that limit from the header sent here: header("Content-length: $dl_size"); As for whether or not this is the issue in this particular case: I don't know. i guess ill just continue playing around with it thanks for giving me a general area to target first >_< i hope someone can just solve it for me though >_< Quote Link to comment https://forums.phpfreaks.com/topic/158672-download-script/#findComment-837014 Share on other sites More sharing options...
MadTechie Posted May 19, 2009 Share Posted May 19, 2009 When i say about the time i refer to the time it takes to download IE if it takes you 15 seconds to download 2MB and 30Seconds to download 4MB but fails at to download anything larger it maybe timing out after 30seconds and not files over 4MB do you have this online at all ? also check the server logs Quote Link to comment https://forums.phpfreaks.com/topic/158672-download-script/#findComment-837494 Share on other sites More sharing options...
random_qwerty Posted May 20, 2009 Author Share Posted May 20, 2009 When i say about the time i refer to the time it takes to download IE if it takes you 15 seconds to download 2MB and 30Seconds to download 4MB but fails at to download anything larger it maybe timing out after 30seconds and not files over 4MB do you have this online at all ? also check the server logs its too fast to count, a few secs at most. the variable is correct for the file size, but i will do more testing there to confirm. it never fails, it just breaks the download at 4kb. im with godaddy, so unfortunately i can't find their logs >_< i use a xamp devlopment environment for hosting while i test Quote Link to comment https://forums.phpfreaks.com/topic/158672-download-script/#findComment-837766 Share on other sites More sharing options...
MadTechie Posted May 20, 2009 Share Posted May 20, 2009 try a hard coded version, this works fine on my WAMPSERVER 5 <?php $dl_details['directory'] = "i:/"; //update $dl_details['file_name'] = "activator.iso"; //update $dl_details['size'] =filesize($dl_details['directory'].$dl_details['file_name']); $last_dl_date = date('H:i') . ' on ' . date('d-m-Y'); $last_dl = $dl_details['file_name']; $last_dl_size = $dl_details['size']; $dl_size = $dl_details['size']; $dl_directory = urldecode($dl_details['directory']); $file_dl = urldecode($dl_details['file_name']); $dl_directory .= $dl_details['file_name']; header("Cache-Control: no-cache"); header("Expires: -1"); header("Content-disposition: attachment; filename=\"$file_dl\""); header("Content-type: application/octet-stream"); header("Content-length: $dl_size"); // set the download rate limit (=> 100 = 100 kb/s) $download_rate = 200; if(file_exists($dl_directory) && is_file($dl_directory)) { // flush content flush(); // open file stream $file_op = fopen($dl_directory, "rb"); while(!feof($file_op)) { // send the current file part to the browser print fread($file_op, round($download_rate * 1024)); // flush the content to the browser set_time_limit(0); ob_flush(); flush(); // sleep one second sleep(1); } // close file stream fclose($file_op); exit; }else{ die('Error: The file '.$dl_directory.' does not exist!'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/158672-download-script/#findComment-837773 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.