[solved] Force Download or any better than readfile()

Discussion in 'PHP' started by redstrike, Jul 9, 2012.

  1. redstrike

    redstrike Member

    I want to force download files instead of letting browsers handle files automaticaly. My files are stored on a remote server. Is there any way better than using readfile() to buffer download?

    I see the Internal Redirect, but it's not working.
    Last edited by a moderator: Jul 11, 2012
  2. webizen

    webizen New Member

  3. redstrike

    redstrike Member

    I have 2 servers, 1 for running PHP apps and database, 1 for streaming and download medium static files.

    I was using this line of PHP code to handle download.

    PHP:
    header('Location: ' $external_media_url);
    It's good in performance but lacking of convenience for my users, IE users must Save target as..., or browsers auto-handle this with their inline media player.

    I've searched around the internet for a coupe of hours, but everyone is using these line of codes for their download feature.

    PHP:
            $filename basename($url).PHP_EOL;
            
    $filesize getRemoteFileSize($url);            
            
    header('Content-Description: File Transfer');        
            
    header('Expires: 0');
            
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            
    header('Pragma: public');
            
    header('Content-Disposition: attachment; filename =' $filename);
            
    header('Content-Length: ' $filesize);
            
    readfile($url);
    This works GREAT, but i see an impact to client's speed rate and long processing time to serve a download, because it must read entire filesize in the external server and re-buffer to serve client.
  4. webizen

    webizen New Member

    if you want to use LSWS internal redirect, you need the download and PHP app on the same server or download being the local path to PHP app (or via nfs mount).
  5. redstrike

    redstrike Member

    I follow this guide: http://drupal.org/node/417866

    AND it works like a charm.
  6. NiteWave

    NiteWave Administrator

    paste the tip here:
    Code:
    in .htaccess:
    <FilesMatch "\.(?i:doc|odf|pdf|rtf|txt)$">
      Header set Content-Disposition attachment
    </FilesMatch>

Share This Page