Serving Files with PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David T. Ashley

    #1

    Serving Files with PHP

    I've used Apache with the automatic directory indexes ... works great ...
    just click on the file name and IE magically figures out how to display it
    .... if it is a .PDF then Adobe Acrobat runs, etc.

    However, I'd like to "serve" files in a more restrictive context where:

    a)The user has to have a valid session identifier in order to get a file.

    b)The files I'd be serving aren't in directories that Apache can get to
    directly, i.e. not in a "Document Root" or aliased directory (although it
    has the right permissions to get the files).

    Is it possible to do this with PHP?

    Can PHP just spit out the right MIME information and then somehow open and
    encode the file and the client will treat it properly?

    Are there any PHP functions that help with the encoding?

    Are there any good web references?

    Thanks



  • Chuck Anderson

    #2
    Re: Serving Files with PHP

    David T. Ashley wrote:
    I've used Apache with the automatic directory indexes ... works great ...
    just click on the file name and IE magically figures out how to display it
    ... if it is a .PDF then Adobe Acrobat runs, etc.
    >
    However, I'd like to "serve" files in a more restrictive context where:
    >
    a)The user has to have a valid session identifier in order to get a file.
    >
    b)The files I'd be serving aren't in directories that Apache can get to
    directly, i.e. not in a "Document Root" or aliased directory (although it
    has the right permissions to get the files).
    >
    Is it possible to do this with PHP?
    >
    Yes.
    Can PHP just spit out the right MIME information
    Yes.
    and then somehow open and encode the file and the client will treat it properly?
    >
    If the mime type is right, the client should be able to treat it properly.
    Are there any PHP functions that help with the encoding?
    >
    No encoding is needed.

    You can add headers with header and serve it with readfile or include.
    Are there any good web references?
    >
    Php.net usually has lots of good notes added by users.

    Here's how I serve a pdf file that exists outside of the site home
    directory.

    $file = 'path to the pdf file';

    header('Content-Description: File Transfer');
    header('Content-Type: application/pdf');
    header('Content-Length: ' . filesize($file) );

    // to open in browser
    header('Content-Disposition: inline; filename=' . basename($file) );

    // to download
    // header('Content-Disposition: attachment; filename=' . basename($file) );

    readfile($file) ; /* or use include($file); */

    --
    *************** **************
    Chuck Anderson • Boulder, CO

    *************** **************

    Comment

    • Pedro Graca

      #3
      Re: Serving Files with PHP

      David T. Ashley wrote:
      a)The user has to have a valid session identifier in order to get a file.
      Why?
      Or, to put it differently, what do you mean "valid session identifier"?
      And what good does it do to you when the "user has one"?
      b)The files I'd be serving aren't in directories that Apache can get to
      directly, i.e. not in a "Document Root" or aliased directory (although it
      has the right permissions to get the files).
      readfile()

      Is it possible to do this with PHP?
      Yes.
      Can PHP just spit out the right MIME information and then somehow open and
      encode the file and the client will treat it properly?
      See mime_content_ty pe()

      Are there any PHP functions that help with the encoding?
      What do you mean?
      Maybe some of the function in http://php.net/recode or
      http://php.net/unicode would help, but I doubt it.
      Are there any good web references?
      Yes. The PHP manual :)



      --
      I (almost) never check the dodgeit address.
      If you *really* need to mail me, use the address in the Reply-To
      header with a message in *plain* *text* *without* *attachments*.

      Comment

      Working...