outputting a file for download

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kingofkolt

    #1

    outputting a file for download

    im trying to let a user download a file from my website, for example a
    picture. i dont want the user to have to right-click and do save-as, but
    instead i want a window to popup (the window with "Save File","Open
    File","More Info",etc) so that the user can download it easily. i have a
    feeling this has something to do with headers, but i have no idea what the
    correct headers are.

    thanks in advance

    - JP


  • Oliver Grätz

    #2
    Re: outputting a file for download

    kingofkolt schrieb:[color=blue]
    > im trying to let a user download a file from my website, for example a
    > picture. i dont want the user to have to right-click and do save-as, but
    > instead i want a window to popup (the window with "Save File","Open
    > File","More Info",etc) so that the user can download it easily. i have a
    > feeling this has something to do with headers, but i have no idea what the
    > correct headers are.
    >[/color]

    /**
    * Send a download
    *
    * Netscape (at least V7) (an perhaps other browsers too) adds
    * a ".php" to the filename of the downloaded file. Mozilla does
    * not show this behaviour. Solution (if applicable) is to use
    * a link like
    *
    * <a href="download. php/?datei='.$file. '">
    *
    * The additional / does the trick.
    *
    *
    * Other solution: use a script with no .php extension, but one
    * with no ending at all. You need a special directory with a
    * ".htaccess" including the line
    *
    * DefaultType: application/php;
    *
    * @param string $filename only the filename
    * @param string $path path to directory of file
    */
    function send_download($ filename='',$pa th='')
    {
    $fullname=$path .$filename;
    /*
    * Headers
    */
    header("Content-Type: application/octet-stream;");
    header('Content-Disposition: attachment;file name='.$filenam e.';');
    header('Content-Transfer-Encoding: binary;');
    header('Content-Length: '.filesize($ful lname).';');

    $f=fopen($fulln ame,'rb');
    fpassthru($f);
    fclose($f);
    }


    I recommend using this with relatively small files (<50 MB) only.
    Apache on Windows shows the behaviour of allocating RAM for the
    whole size of the file, no matter what tricks you try to use in
    order to prevent this (buffer-flushing during send etc).


    AllOlli



    Comment

    • Cecile Muller

      #3
      Re: outputting a file for download

      > im trying to let a user download a file from my website, for example a[color=blue]
      > picture. i dont want the user to have to right-click and do save-as, but
      > instead i want a window to popup (the window with "Save File","Open
      > File","More Info",etc) so that the user can download it easily.[/color]


      Before any output (otherwise headers will be sent before your code),
      you could use this function for example:

      promptFileDownl oad("http://www.site.com/file.jpg"); //you can use
      either a local path or an url


      The body of the function:

      function promptFileDownl oad($chemin){
      if ((!file_exists( $chemin)) && (!@fclose(@fope n($chemin, "r"))))
      die('Error:
      invalid path to the file to download');
      $filename = stripslashes(ba sename($chemin) );
      $user_agent = strtolower($_SE RVER["HTTP_USER_AGEN T"]);
      header("Content-type: application/force-download");
      header(((is_int eger(strpos($us er_agent,
      "msie")))&&(is_ integer(strpos( $user_agent,
      "win"))))?"Cont ent-Disposition:
      filename=\"$fil ename\"":"Conte nt-Disposition: attachment;
      filename=\"$fil ename\"");
      header("Content-Description: File download");
      @readfile($chem in);
      die();
      }

      (vote for it at: http://www.nexen.net/scripts/details...pts=960&vote=5)


      PS: better copy/paste the code from the linked page isntead of the one
      posted here because of Googlegroups's word wrapping.

      Comment

      • Chung Leong

        #4
        Re: outputting a file for download

        "Oliver Grätz" <oliver.graetz@ gmx.de> wrote in message
        news:408541ba$0 $15497$9b4e6d93 @newsread4.arco r-online.net...[color=blue]
        >
        > I recommend using this with relatively small files (<50 MB) only.
        > Apache on Windows shows the behaviour of allocating RAM for the
        > whole size of the file, no matter what tricks you try to use in
        > order to prevent this (buffer-flushing during send etc).[/color]

        PHP uses memory-mapped file for file streams on Win32, I think. The file is
        mapped into the application's virtual memory space, so it appears that it's
        eating up a lot of memory, but only a fraction of that is physical memory.


        Comment

        • kingofkolt

          #5
          Re: outputting a file for download

          "Cecile Muller" <spam@wildpeaks .com> wrote in message
          news:aff31ac6.0 404201250.5759b f7e@posting.goo gle.com...[color=blue][color=green]
          > > im trying to let a user download a file from my website, for example a
          > > picture. i dont want the user to have to right-click and do save-as, but
          > > instead i want a window to popup (the window with "Save File","Open
          > > File","More Info",etc) so that the user can download it easily.[/color]
          >
          >
          > Before any output (otherwise headers will be sent before your code),
          > you could use this function for example:
          >
          > promptFileDownl oad("http://www.site.com/file.jpg"); //you can use
          > either a local path or an url
          >
          >
          > The body of the function:
          >
          > function promptFileDownl oad($chemin){
          > if ((!file_exists( $chemin)) && (!@fclose(@fope n($chemin, "r"))))
          > die('Error:
          > invalid path to the file to download');
          > $filename = stripslashes(ba sename($chemin) );
          > $user_agent = strtolower($_SE RVER["HTTP_USER_AGEN T"]);
          > header("Content-type: application/force-download");
          > header(((is_int eger(strpos($us er_agent,
          > "msie")))&&(is_ integer(strpos( $user_agent,
          > "win"))))?"Cont ent-Disposition:
          > filename=\"$fil ename\"":"Conte nt-Disposition: attachment;
          > filename=\"$fil ename\"");
          > header("Content-Description: File download");
          > @readfile($chem in);
          > die();
          > }
          >
          > (vote for it at:[/color]
          http://www.nexen.net/scripts/details...pts=960&vote=5)[color=blue]
          >
          >
          > PS: better copy/paste the code from the linked page isntead of the one
          > posted here because of Googlegroups's word wrapping.[/color]

          ok ill try that.. thanks for the help all

          - JP


          Comment

          Working...