file download through php

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aljosa.mohorovic@gmail.com

    file download through php

    I have a problem when doing indirect download of file through php, when
    I click on download link Firefox and Internet Explorer give me same
    options: Open and Save. Firefox opens file directly from Internet and
    downloads without problem.

    Internet Explorer saves file on local disk and I can open it from disk
    correctly but when I try to open it directly from Internet it responds
    with "There was an error opening this document. This file cannot be
    found.", concrete situation in my usage is Pdf document. Below is my
    concrete code, any suggestions are appreciated.


    <?php
    ob_start();
    include_once("c onfig.php");
    ini_set('displa y_errors', 'Off');
    error_reporting (0);

    $args = trim($_SERVER["PATH_INFO"]);
    $args = preg_replace("/^\//i", '', $args);
    $argv = explode('/', $args);
    $id = (int)$argv[0];

    import('dao.fm. file');
    $f = fm_file::instan ce($id);

    if($f->get('id') < 1) {
    die('No file.');
    }
    header('Content-Disposition: attachment');
    header('Accept-Charset: UTF-8');
    header('Content-Type: '.$f->get('type'). '; charset="UTF-8";');
    readfile(get('F ILES').'/'.$f->get('id'));
    ob_end_flush();
    ?>

  • NC

    #2
    Re: file download through php

    aljosa.mohorovi c@gmail.com wrote:[color=blue]
    >
    > I have a problem when doing indirect download of file through php, when
    > I click on download link Firefox and Internet Explorer give me same
    > options: Open and Save. Firefox opens file directly from Internet and
    > downloads without problem.
    >
    > Internet Explorer saves file on local disk and I can open it from disk
    > correctly but when I try to open it directly from Internet it responds
    > with "There was an error opening this document. This file cannot be
    > found.", concrete situation in my usage is Pdf document. Below is my
    > concrete code, any suggestions are appreciated.[/color]

    OK, I can see several possible issues here:
    [color=blue]
    > header('Content-Disposition: attachment');[/color]

    I think you're supposed to specify a file name here:

    header("Content-Disposition: attachment;
    filename=[file_name_goes_ here]");
    [color=blue]
    > header('Accept-Charset: UTF-8');[/color]

    I am not sure "Accept-Charset:" header is appropriate when serving
    binary files... Comment this line out and see what happens...
    [color=blue]
    > header('Content-Type: '.$f->get('type'). '; charset="UTF-8";');[/color]

    I am not sure that "charset" field is appropriate within
    "Content-Type:" header when the content type it not "text/*". In any
    case, there should be no semicolon at the end of a header... Try
    changing it to this:

    header('Content-Type: ' . $f->get('type')) ;

    Cheers,
    NC

    Comment

    • Chung Leong

      #3
      Re: file download through php

      aljosa.mohorovi c@gmail.com wrote:[color=blue]
      > I have a problem when doing indirect download of file through php, when
      > I click on download link Firefox and Internet Explorer give me same
      > options: Open and Save. Firefox opens file directly from Internet and
      > downloads without problem.
      >
      > Internet Explorer saves file on local disk and I can open it from disk
      > correctly but when I try to open it directly from Internet it responds
      > with "There was an error opening this document. This file cannot be
      > found.", concrete situation in my usage is Pdf document. Below is my
      > concrete code, any suggestions are appreciated.
      >
      >
      > <?php
      > ob_start();
      > include_once("c onfig.php");
      > ini_set('displa y_errors', 'Off');
      > error_reporting (0);
      >
      > $args = trim($_SERVER["PATH_INFO"]);
      > $args = preg_replace("/^\//i", '', $args);
      > $argv = explode('/', $args);
      > $id = (int)$argv[0];
      >
      > import('dao.fm. file');
      > $f = fm_file::instan ce($id);
      >
      > if($f->get('id') < 1) {
      > die('No file.');
      > }
      > header('Content-Disposition: attachment');
      > header('Accept-Charset: UTF-8');
      > header('Content-Type: '.$f->get('type'). '; charset="UTF-8";');
      > readfile(get('F ILES').'/'.$f->get('id'));
      > ob_end_flush();
      > ?>[/color]

      The problem you encountered has to do with the dumb way IE's caching
      mechanism works. If it receives a no-cache directive from the server,
      IE would immediately remove the file after it's downloaded. The
      external program doesn't get a chance to open it.

      Add this and see if that fixes the problem:

      header('Cache-Control: ');

      Comment

      • John Dunlop

        #4
        Re: file download through php

        NC:
        [color=blue]
        > aljosa.mohorovi c@gmail.com wrote:
        >[color=green]
        > > header('Content-Disposition: attachment');[/color]
        >
        > I think you're supposed to specify a file name here:[/color]

        RFC2183 says the filename parameter is optional, though whether that
        has any bearing on what you are supposed to include in the header
        fields of an HTTP message is another question.
        [color=blue][color=green]
        > > header('Accept-Charset: UTF-8');[/color]
        >
        > I am not sure "Accept-Charset:" header is appropriate when serving
        > binary files...[/color]

        Since Accept-Charset is defined only as a request-header, it is
        meaningless in a response (unless there is a private agreement which
        assigns it a meaning).

        --
        Jock

        Comment

        • csrster@gmail.com

          #5
          Re: file download through php


          Chung Leong wrote:[color=blue]
          > aljosa.mohorovi c@gmail.com wrote:[color=green]
          > > I have a problem when doing indirect download of file through php, when
          > > I click on download link Firefox and Internet Explorer give me same
          > > options: Open and Save. Firefox opens file directly from Internet and
          > > downloads without problem.
          > >
          > > Internet Explorer saves file on local disk and I can open it from disk
          > > correctly but when I try to open it directly from Internet it responds
          > > with "There was an error opening this document. This file cannot be
          > > found.", concrete situation in my usage is Pdf document. Below is my
          > > concrete code, any suggestions are appreciated.
          > >
          > >
          > > <?php
          > > ob_start();
          > > include_once("c onfig.php");
          > > ini_set('displa y_errors', 'Off');
          > > error_reporting (0);
          > >
          > > $args = trim($_SERVER["PATH_INFO"]);
          > > $args = preg_replace("/^\//i", '', $args);
          > > $argv = explode('/', $args);
          > > $id = (int)$argv[0];
          > >
          > > import('dao.fm. file');
          > > $f = fm_file::instan ce($id);
          > >
          > > if($f->get('id') < 1) {
          > > die('No file.');
          > > }
          > > header('Content-Disposition: attachment');
          > > header('Accept-Charset: UTF-8');
          > > header('Content-Type: '.$f->get('type'). '; charset="UTF-8";');
          > > readfile(get('F ILES').'/'.$f->get('id'));
          > > ob_end_flush();
          > > ?>[/color]
          >
          > The problem you encountered has to do with the dumb way IE's caching
          > mechanism works. If it receives a no-cache directive from the server,
          > IE would immediately remove the file after it's downloaded. The
          > external program doesn't get a chance to open it.
          >
          > Add this and see if that fixes the problem:
          >
          > header('Cache-Control: ');[/color]

          I am wrestling with a very similar problem. Here is some of my php
          script (after taking the
          above suggestion on board):

          Header("Content-Type: application/octet-stream ");
          Header("Content-Disposition: attachment; filename=\"$fil ename\"");
          Header("Content-Length: $length");
          Header("Cache-Control: ");

          clip_tv();
          while (@ob_end_flush( ));
          exit();

          function clip_tv() {
          global $startTime, $duration, $fileLocation, $bitrate;
          $startBytes=$st artTime*$bitrat e;
          $cutterCommand = "/home/fedora/bin/mencoder 3>&1 1>&2 -quiet -sb
          $startBytes -endpos $duration -oac copy -ovc copy -of mpeg
          $fileLocation -o /proc/self/fd/3 2>/dev/null";
          passthru($cutte rCommand);
          }

          I (left-) click on a link which calls this script with some request
          parameters. IE pops up
          a box asking me to open or save. I select "save" and browse to a file.
          IE downloads until
          it reaches, supposedly, 99% of the total stream length and then "Cannot
          copy file. Source
          file or disk is unreadable" (my translation).

          Firefox is no problem. Incidentally, IE also ignores the suggested
          filename in the Content-disposition header.

          --
          Colin Rosenthal

          Comment

          • aljosa.mohorovic@gmail.com

            #6
            Re: file download through php

            "IE also ignores the suggested filename in the Content-disposition
            header" and also Safari will not properly display non latin1 charsets.
            my solution for download link:
            URL: http://www.example.com/download.php/$file_id/$file_name
            1. download.php can be replaced using mod_rewrite into something more
            readable
            2. $file_id can be location of file or anything else
            3. $file_name is used only to indicate filename to browser and can be
            string with any character and IE,Firefox and Safari will display
            properly filename for download

            and about IE strange problem? would like to know how to fix it but
            because i have no time I've gone with "inline" instead "attachment " for
            Content-Disposition.

            Comment

            • LeonardoCA

              #7
              Re: file download through php

              For IE is needed:

              header("Pragma: public");

              see:


              Comment

              Working...