Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    N. Ireland
    Posts
    351
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Forcing Downloads

    Hi guys,

    Trying to use PHP to download a pdf from server to local machine.
    Got the following:
    PHP Code:
    $bs Singleton::getInstance('BrowserSniffer');
        
    $file $_SERVER['DOCUMENT_ROOT'] . '/pdf/' $location;
        
    $filename substr(strrchr($file'/'), 1);
        if (
    $bs->name == 'Mozilla') {
          
    $content_type "application/octet-stream\n";
          
    $attachment ' attachment; ';
        } else {
          
    $content_type "application/download\n";
          
    $attachment ' ';
        }
        
    header('Content-Type: ' $content_type);
        
    header('Content-Transfer-Encoding: binary');
        
    header('Content-Disposition: ' $attachment ' filename=' $filename);
        
    header('Content-Length: ' filesize($file));
        
    $fn fopen($file'r');
        
    fpassthru($fn); 
    But cannot get the file to download if $location has spaces. Is there any way around this?

    D.
    Daemonkin.
    If this was helpful, please add to my reputation
    Thousand Sons - Freelance Web Developer - ninetyonedegrees.com

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    Are you passing file names in the url.

    if that is the case, than all non-alphanumeric characters except -_. would be replace with "%"...and as a result, you are referencing a file that does not exist on your system. Therefore, I would strongly recommend that you ensure that the referenced file exists on the file system first, and then force the download.
    also, you should use functions such as urldecode to remove any encoded file name if you are passing values in the url.

    hope that helps.

    Cheers,
    Ess

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    urlencode($location) should work.

  • #4
    Regular Coder
    Join Date
    Jun 2007
    Location
    N. Ireland
    Posts
    351
    Thanks
    16
    Thanked 4 Times in 4 Posts
    I am getting $location in the function from SQL.
    I will try the urlencode anyway.

    D.

    EDIT: Used the urlencode function on:
    PHP Code:
    header('Content-Disposition: ' $attachment ' filename=' urlencode($filename)); 
    And apart from spaces being transformed to '+' the pdf files download and work.

    Thanks.
    Last edited by daemonkin; 08-21-2007 at 08:43 AM.
    Daemonkin.
    If this was helpful, please add to my reputation
    Thousand Sons - Freelance Web Developer - ninetyonedegrees.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •