CodeIgniter Forums

Full Version: download remote file with resumable capability
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i need to scrip to download remote file with resumable capability . i write this code but notworking true. 
PHP Code:
$CI =& get_instance();
$CI->load->helper('func');
$CI->load->helper('download');
if(
$fileData !== false){
 
   $timeexpire $fileData['time'];
 
   $now time();
 
   $file '';
 
   if($timeexpire $now){
 
       redirect('download/time/2');
 
   }else{
 
       $url              str_replace(" ","%20",$fileData['url']);
 
       $fileinfo pathinfo($url);
 
       $mimType= ($fileinfo['extension'] == 'mp3') ? 'audio/mpeg' 'txt';//set mime type to mp3
 
       $fileSize curl_get_file_size($url);//get file size with curl
 
       $foundCode existfile($url);//check exists file or not and return 400 or 200          
 
       if($foundCode == 200){
 
          $handel        = @fopen($url,'r');
 
       }else{
 
            redirect(base_url().'track/faild'); 
 
                      
        $begin    
1;
 
       $end      $fileSize ;
 
       $fileName basename($url);
 
       if (isset($_SERVER['HTTP_RANGE'])){
 
           if (preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i'$_SERVER['HTTP_RANGE'], $matches))
 
           {
 
               $begin  intval($matches[1]);
 
               if (!empty($matches[2]))
 
               {
 
                 $end  intval($matches[2]);
 
               }
 
           }
 
       }
 
       if (isset($_SERVER['HTTP_RANGE']))
 
       {
 
         header('HTTP/1.1 206 Partial Content');
 
       }
 
       else
        
{
 
        header('HTTP/1.1 200 OK');
 
       }
 
       header('Pragma: public');     // required
 
       header('Accept-Ranges: bytes'); 
 
       header("Content-Type: $mimType");        
        header
("Content-Disposition: attachment; filename=$fileName");
 
       header('Cache-Control: private');        
        header
("Content-Range: bytes $begin-$end/$fileSize");
 
       header('Content-Length:' $fileSize);        
        $cur  
$begin;
 
       fseek($handel,$cur,0);
 
       if($handel != NULL){                        
            while
(!feof($handel))
 
                                       
                print fread
($handel1024);
 
               //$cur += 1024*16;
 
           }
 
       }else{
 
           redirect(base_url()."track/faild");
 
       }
 
              

Look what Bing found for you...

https://stackoverflow.com/questions/1626...ith-resume

That Bing seems to think there are lots of answers for this out there. It says it found 1,690,000 results. This internet thing is amazing, I am surprised not everyone uses it...
apache (ubuntu, debian)
apt-get install libapache2-mod-xsendfile
https://tn123.org/mod_xsendfile/

nginx
https://www.nginx.com/resources/wiki/sta...xsendfile/
I used this code but did not allow downloading by download softwares  like (idm and ...).
[attachment=895]
(06-25-2017, 02:20 AM)programmer Wrote: [ -> ]I used this code but did not allow downloading by download softwares  like (idm and ...).

xsendfile or the code from PaulD
(06-25-2017, 04:39 AM)Paradinight Wrote: [ -> ]
(06-25-2017, 02:20 AM)programmer Wrote: [ -> ]I used this code but did not allow downloading by download softwares  like (idm and ...).

xsendfile or the code from PaulD

code from PaulD

xsendfile not support to share host
Hi programmer,

You sent me a private message but by default private messaging is switched off so I cannot reply to you. You have to turn on private messaging in your profile.

Anyway, in response, indirect download simply refers to the fact that a link you provide is not actually a link to the file, otherwise it can be shared for all to download. This is achieved relatively easily with CI.

The link you provide is something like:
PHP Code:
/downloads/download_file/this_is_the_name_of_the_file 

Your controller then checks first that the user is allowed to download the file (however you are doing your user authentication and authorisation), then you use CI download helper to read the file (which is kept outside public_html or in a file protected from direct access with file permissions and htaccess) and send download headers and the file contents so the users browser does a download and not a page view. The download helper is very easy to use and does all this for you.

The only thing you have to do is to have a table to decode the provided "this_is_the_name_of_the_file" to the actual file name kept on the server. So you might have:

PHP Code:
/downloads/download_file/online_banking_report 

But "online_banking_report" is decoded to refer to the filename "banking_report_july_2017.pdf". A direct link to the file is never shown to anyone, so there is nothing to share. And if you keep it outside public_html then only the server itself can find and read it anyway.

PHP Code:
force_download('/path/to/banking_report_july_2017.pdf'TRUE); 

What you were asking about originally, about resuming an interrupted download, is much more complex IMHO, as it depends so much on your webserver, the device the user is using and the browser.

Hope that helps,

Paul.

EDIT: PS If you knew all that already, and this is a simplistic answer for you, I apologise in advance :-)
Did not mean to patronise if this is all bloody obvious. Sorry.
When the user clicks on each file. A key is stored with the file address in the database. Next, the file is read by url and the headers are changed and ready for download.

I test the way you said. Thank you