SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    New Zealand
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Another download restriction question

    I've seen a few similar forum topics, but none have answered my question satisfactorily.

    Very simply, we will have a folder for each user containing pdf files they can download.

    Something like this:
    www.example.com/files/fred/quote060504.pdf
    www.example.com/files/joe/quote060505.pdf

    If Joe guesses the name of Fred's file is there any way to prevent him viewing it?

    OR, can we put the files below the web tree and pull them somehow as required?

    Note: We can only use scripted authentication, not htaccess (unless the htaccess can be overridden/bypassed by a script).

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    New Zealand
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Never mind

    I think I've figured it out....

    The following causes the pdf file (that is below web root) to be displayed in my browser. I wonder if it will automatically prompt for a download in a browser that does not support display of pdfs.

    PHP Code:
    <?php
    //get_pdf.php
    session_start();
    $file_base='/home/somewhere_below_web/files/'.$_SESSION['user_id']."/";

    if (
    file_exists($file_base.$_GET['file'])) 
    {
        
    header('content-disposition: inline; filename='.$_GET['file']);
        
    header('content-type: application/pdf');
        
    header('content-length:'.filesize($file_base.$_GET['file']));

        
    readfile($file_base.$_GET['file']);

    else 
    {
        
    //file does not exist in specified folder
    }
    ?>
    HTML Code:
    <!-- Private HTML file only accessible when logged in -->
    <a href="get_pdf.php?file=secret_file.pdf">secret_file.pdf</a>
    I guess I would put the script below web root too and include it.

    It could also be expanded to test the file extension and set the mime type accordingly.

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    New Zealand
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by KiwiJohn
    I wonder if it will automatically prompt for a download in a browser that does not support display of pdfs.
    Just tried it in Opera and it does prompt for a download.

  4. #4
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $current UPLOADDIR.$current;

    if( 
    file_exists$current ) )
    {
        
    header("Cache-Control: no-store, no-cache, must-revalidate");
        
    header("Cache-Control: post-check=0, pre-check=0"false);
        
    header("Expires: " .gmdate("D, d M H:i:s",mktime(date("H")+2,date("i"),date("s"),date("m"),date("d"),date("Y"))) . " GMT");
        
    header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
        
    header("Content-Type: application/force-download");
        
    header("Content-Type: application/download");
        if(
    preg_match('#Opera(/| )([0-9].[0-9]{1,2})#',getenv('HTTP_USER_AGENT')) or preg_match('#MSIE ([0-9].[0-9]{1,2})#',getenv('HTTP_USER_AGENT')))
            
    header("Content-Type: application/octetstream");
        else
            
    header("Content-Type: application/octet-stream");
        
    header("Pragma: public");
        
    header("Pragma: no-cache");
        
    header("Cache-Control: private",false);
        
    header('Content-Disposition: attachment; filename='basename($original));
        
    header("Content-Transfer-Encoding: binary");
        
    header("Content-Length: " filesize($current));
        
    $bytes readfile($current);
    }
    else
    {
        
    WRITE_LOG__FILE____LINE__"Could not open file $current."true );
        echo 
    "Error: Could not find file.";

    This code should force a download prompt for every file type, regardless if the browser normally displays it inline.

  5. #5
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    New Zealand
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by UFTimmy
    This code should force a download prompt for every file type, regardless if the browser normally displays it inline.
    Awesome! Thanks for that!


Bookmarks

Posting Permissions

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