what`s wrong with this .htaccess file ?

Discussion in 'Apache' started by GaGa25, Oct 5, 2009.

  1. #1
    Options All -Indexes
    AddType application/octet-stream .pdf
    Code (markup):
    the force download is not working for the pdf file , so what is wrong with this file ?
     
    GaGa25, Oct 5, 2009 IP
  2. GaGa25

    GaGa25 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    anyone can help plz ?
     
    GaGa25, Oct 5, 2009 IP
  3. GaGa25

    GaGa25 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    it didn`t work
     
    GaGa25, Oct 5, 2009 IP
  4. ramnet

    ramnet Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    In order to force downloading of a file you will need to use the Content-Disposition header, which tells the browser what to do with the file.

    The most common way of setting this header is by using a download script - here's a simple one I whipped up just for you (released into public domain):

    
    <?php
    $file=$_REQUEST['file'].'.pdf';
    if(file_exists($file)==false){die;}
    if(strstr($file,'..')!=false || substr($file,0,1)=='/'){die;} # security measure
    header('Content-disposition: attachment; filename='.basename($file));
    header('Content-type: application/octet-stream');
    header('Content-Length: '.filesize($file));
    readfile($file);
    ?>
    
    PHP:
    upload above script into your download directory and name it pdf.php.

    change links to your pdf files like so:

    my-pdf-file.pdf
    -- becomes --
    pdf.php?file=my-pdf-file

    ---

    If you don't want to use a script there may be a way to send the needed Content-Disposition header using the Header directive from the mod_headers module - just add it to the .htaccess file you already have:

    
    Options All -Indexes
    AddType application/octet-stream .pdf
    Header set Content-Disposition "attachment; filename=file.pdf"
    
    Code (markup):
    Note that this 2nd method won't work if you have non-pdf files in the directory / subdirectory.

    Just be sure not to do both methods!
     
    ramnet, Oct 6, 2009 IP
  5. GaGa25

    GaGa25 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    actually i found a way to make the htaccess works but except IE ? this is it
    Options All -Indexes
    <FilesMatch "\.(?i:pdf)$">
      ForceType application/octet-stream
      Header set Content-Disposition attachment
    </FilesMatch>[B][/B]
    Code (markup):
     
    GaGa25, Oct 7, 2009 IP