akshay Posted July 23, 2010 Share Posted July 23, 2010 Hi. I use this script in PHP to launch downloads. Works fine. But, I wanna make downloads resumable. Any suggestions? Please be very helpful. I really gotta do it with a simple code. <?php $fullPath="any/path/to/file.ext"; $fd = fopen ($fullPath, "r"); if ($fd) { $fsize = filesize($fullPath); $path_parts = pathinfo($fullPath); $ext = strtolower($path_parts["extension"]); switch ($ext) { case "pdf": header("Content-type: application/pdf"); // add here more headers for diff. extensions header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download header("Content-Length: $fsize;\n"); break; default; header("Content-type: application/octet-stream"); header("Content-Disposition: filename=\"".$path_parts["basename"]."\""); header("Content-Length: $fsize;\n"); } header("Content-length: $fsize"); header("Cache-control: private"); //use this to open files directly while(!feof($fd)) { $buffer = fread($fd, 2048); echo $buffer; } } fclose ($fd); exit; // example: place this kind of link into the document where the file download is offered: // <a href="download.php?download_file=some_file.pdf">Download here</a> ?> -I've already been to http://stackoverflow.com/questions/157318/resumable-downloads-when-using-php-to-send-the-file but found info was incomplete (if u could complete it... or so) - I donot want a complex script as offered by thomthom.net -I dont understand using header("Partial-Content:"); (i mean two variables passed offset, and remaining size) I'd really be thankful if you can help me with a simple code for supporting Resumable Downloads. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/208687-resumable-downloading/ Share on other sites More sharing options...
akshay Posted July 23, 2010 Author Share Posted July 23, 2010 Other way i can do is..copy a file to a temporary path, provide resumable download (direct link), and clear link in about 3-4 days. But thats not professional and would increase server load. Please help. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/208687-resumable-downloading/#findComment-1090245 Share on other sites More sharing options...
radar Posted July 23, 2010 Share Posted July 23, 2010 http://stackoverflow.com/questions/157318/resumable-downloads-when-using-php-to-send-the-file has some rather interesting ideas on this subject. perhaps that will help you get started. Quote Link to comment https://forums.phpfreaks.com/topic/208687-resumable-downloading/#findComment-1090250 Share on other sites More sharing options...
akshay Posted July 24, 2010 Author Share Posted July 24, 2010 I like the ideas. But they are incomplete chunks of code. Can someone complete the code (would require hardly 2-3 lines) Thank you Quote Link to comment https://forums.phpfreaks.com/topic/208687-resumable-downloading/#findComment-1090546 Share on other sites More sharing options...
joel24 Posted July 24, 2010 Share Posted July 24, 2010 I like the ideas. But they are incomplete chunks of code. Can someone complete the code (would require hardly 2-3 lines) Thank you .. why can't you? Quote Link to comment https://forums.phpfreaks.com/topic/208687-resumable-downloading/#findComment-1090577 Share on other sites More sharing options...
akshay Posted July 24, 2010 Author Share Posted July 24, 2010 Coz I dont know. I'm a PHP intermediate and not pro. And I've not much idea about header("___"); commands, except for Location: and Refresh: It's just my guess it would require 2-3 lines... Please help Thanks Quote Link to comment https://forums.phpfreaks.com/topic/208687-resumable-downloading/#findComment-1090579 Share on other sites More sharing options...
akshay Posted July 24, 2010 Author Share Posted July 24, 2010 I've modified code at StackOverflow link. Yet errors occur. Code: $file="path/to/file.ext"; $filesize = filesize($file); $offset = 0; $length = $filesize; if ( isset($_SERVER['HTTP_RANGE']) ) { // if the HTTP_RANGE header is set we're dealing with partial content $partialContent = true; // find the requested range // this might be too simplistic, apparently the client can request // multiple ranges, which can become pretty complex, so ignore it for now preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches); $offset = intval($matches[1]); $length = intval($matches[2]) - $offset; } else { $partialContent = false; } $file = fopen($file, 'r'); // seek to the requested offset, this is 0 if it's not a partial content request fseek($file, $offset); $data = fread($file, $length); fclose($file); if ( $partialContent ) { // output the right headers for partial content header('HTTP/1.1 206 Partial Content'); header('Content-Range: bytes ' . $offset . '-' . ($offset + $length) . '/' . $filesize); } // output the regular HTTP headers header('Content-Type: ' . $ctype); header('Content-Length: ' . $filesize); header('Content-Disposition: attachment; filename="' . $fileName . '"'); header('Accept-Ranges: bytes'); // don't forget to send the data too print($data); Error: ERROR The requested URL could not be retrieved While trying to process the request: GET /dl/resdl.php?f=abcc182&dlpath=f63e91d899787c647670da3fe1786484&sessid=18460a3a23d2935a856dc1ba0e2c4e11 HTTP/1.1 Host: mydomain.com User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 115 Connection: keep-alive Cookie: PHPSESSID=da19dd87e2b5862cdadd788dc3dd0d37 The following error was encountered: * Invalid Response The HTTP Response message received from the contacted server could not be understood or was otherwise malformed. Please contact the site operator. Your cache administrator may be able to provide you with more details about the exact nature of the problem if needed. Your cache administrator is webmaster. Generated Sat, 24 Jul 2010 09:49:37 GMT by sv18.byethost18.org (squid/2.7.STABLE9) Quote Link to comment https://forums.phpfreaks.com/topic/208687-resumable-downloading/#findComment-1090582 Share on other sites More sharing options...
Tazerenix Posted July 24, 2010 Share Posted July 24, 2010 $fileName hasn't been defined and thus defaults to null, which tells the script that there is no file to load. Use $file instead of $fileName and it should work. Quote Link to comment https://forums.phpfreaks.com/topic/208687-resumable-downloading/#findComment-1090589 Share on other sites More sharing options...
akshay Posted July 24, 2010 Author Share Posted July 24, 2010 =( Still not working. Quote Link to comment https://forums.phpfreaks.com/topic/208687-resumable-downloading/#findComment-1090596 Share on other sites More sharing options...
Tazerenix Posted July 24, 2010 Share Posted July 24, 2010 the same error? Quote Link to comment https://forums.phpfreaks.com/topic/208687-resumable-downloading/#findComment-1090598 Share on other sites More sharing options...
akshay Posted July 24, 2010 Author Share Posted July 24, 2010 yep Quote Link to comment https://forums.phpfreaks.com/topic/208687-resumable-downloading/#findComment-1090599 Share on other sites More sharing options...
Tazerenix Posted July 24, 2010 Share Posted July 24, 2010 hmm, echo out the $file variable before sending the content-disposition header and see what the value is EDIT: ahh of course, you are reassigning $file as a file handle. before doing $file = fopen(... assign $fileName = $file; and then use $fileName when calling the content-disposition header. That should fix it Quote Link to comment https://forums.phpfreaks.com/topic/208687-resumable-downloading/#findComment-1090605 Share on other sites More sharing options...
akshay Posted July 24, 2010 Author Share Posted July 24, 2010 not working/ same error. anyway. thanks a lot ! Quote Link to comment https://forums.phpfreaks.com/topic/208687-resumable-downloading/#findComment-1090609 Share on other sites More sharing options...
radar Posted July 26, 2010 Share Posted July 26, 2010 just as a test try $data -- i havent ever needed or wanted to do anything like this so i won't be of true help but i can give some possibilities and hope they work... so try $data instead of $fileName or $file... since you reset $file with $file = fopen(); but you set data when you actually read the file.. so perhaps that's your key? Quote Link to comment https://forums.phpfreaks.com/topic/208687-resumable-downloading/#findComment-1091307 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.