Php Download script problem!

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

    Php Download script problem!

    Hi All

    I have a simple download script for files on my website and I use the code
    below to download the files:

    $fp = fopen($filename , "rb");
    if ( $fp )
    {
    $filesize = $file['size'];

    header("Cache-Control: post-check=0, pre-check=0");
    header("Expires : 0");
    header("Content-Type: " . $xtype);
    header("Content-Length: " . (string)($files ize));
    header("Content-Transfer-Encoding: binary");

    while ( !feof($fp) )
    {
    echo(fgets($fp, 4096));
    }
    fclose($fp);
    }

    Now this works just fine on Firefox and Opera, and even on IE when its used
    in a web page ie <img src="download.p hp?id=2"/> but if I try and go to a
    file directly by typing in the url www.website.com/download.php?id=2 then it
    opens a Download dialog asking me to save the file instead of displaying it
    in the browser, this only happens with IE though. Am I missing a vital bit
    to the header, or is this a problem with IE, if so is there a way around it
    at all?

    Many thanks.


  • Philip Ronan

    #2
    Re: Php Download script problem!

    "C16" wrote:
    [color=blue]
    > $fp = fopen($filename , "rb");
    > if ( $fp )
    > {
    > $filesize = $file['size'];
    >
    > header("Cache-Control: post-check=0, pre-check=0");
    > header("Expires : 0");
    > header("Content-Type: " . $xtype);
    > header("Content-Length: " . (string)($files ize));
    > header("Content-Transfer-Encoding: binary");
    >
    > while ( !feof($fp) )
    > {
    > echo(fgets($fp, 4096));
    > }
    > fclose($fp);
    > }[/color]

    Assuming $xtype is defined correctly somewhere, I reckon IE is choking on
    the Content-Transfer-Encoding header. This is a MIME header -- it doesn't
    belong in an HTTP response at all. Go and read RFC 2616 some time.

    You don't need to cast $filesize to a string -- PHP will do this for you
    anyway. And while you're at it, I suggest you replace the while() loop with
    a readfile() statement instead (<http://uk.php.net/readfile>).

    Oh, by the way, filesize() is another useful function. That way you can get
    rid of the fopen and fclose instructions altogether.

    Phil

    --
    phil [dot] ronan @ virgin [dot] net



    Comment

    • C16

      #3
      Re: Php Download script problem!

      I was using readfile but found a discussion that showed readfile to be '55%'
      slower than reading by chunks (in the readfile function desc on php.net
      infact). Also tried the header without Content-Transfer-Encoding and many
      other permutations from suggestions found via google but still the same
      result on IE.

      "Philip Ronan" <invalid@invali d.invalid> wrote in message
      news:BF743985.3 9633%invalid@in valid.invalid.. .[color=blue]
      > "C16" wrote:
      >[color=green]
      >> $fp = fopen($filename , "rb");
      >> if ( $fp )
      >> {
      >> $filesize = $file['size'];
      >>
      >> header("Cache-Control: post-check=0, pre-check=0");
      >> header("Expires : 0");
      >> header("Content-Type: " . $xtype);
      >> header("Content-Length: " . (string)($files ize));
      >> header("Content-Transfer-Encoding: binary");
      >>
      >> while ( !feof($fp) )
      >> {
      >> echo(fgets($fp, 4096));
      >> }
      >> fclose($fp);
      >> }[/color]
      >
      > Assuming $xtype is defined correctly somewhere, I reckon IE is choking on
      > the Content-Transfer-Encoding header. This is a MIME header -- it doesn't
      > belong in an HTTP response at all. Go and read RFC 2616 some time.
      >
      > You don't need to cast $filesize to a string -- PHP will do this for you
      > anyway. And while you're at it, I suggest you replace the while() loop
      > with
      > a readfile() statement instead (<http://uk.php.net/readfile>).
      >
      > Oh, by the way, filesize() is another useful function. That way you can
      > get
      > rid of the fopen and fclose instructions altogether.
      >
      > Phil
      >
      > --
      > phil [dot] ronan @ virgin [dot] net
      > http://vzone.virgin.net/phil.ronan/
      >
      >[/color]


      Comment

      • Chuck Anderson

        #4
        Re: Php Download script problem!

        C16 wrote:
        [color=blue]
        >I was using readfile but found a discussion that showed readfile to be '55%'
        >slower than reading by chunks (in the readfile function desc on php.net
        >infact). Also tried the header without Content-Transfer-Encoding and many
        >other permutations from suggestions found via google but still the same
        >result on IE.
        >
        >"Philip Ronan" <invalid@invali d.invalid> wrote in message
        >news:BF743985. 39633%invalid@i nvalid.invalid. ..
        >
        >[color=green]
        >>"C16" wrote:
        >>
        >>
        >>[color=darkred]
        >>>$fp = fopen($filename , "rb");
        >>>if ( $fp )
        >>>{
        >>>$filesize = $file['size'];
        >>>
        >>>header("Cach e-Control: post-check=0, pre-check=0");
        >>>header("Expi res: 0");
        >>>header("Cont ent-Type: " . $xtype);
        >>>header("Cont ent-Length: " . (string)($files ize));
        >>>header("Cont ent-Transfer-Encoding: binary");
        >>>
        >>>while ( !feof($fp) )
        >>>{
        >>>echo(fgets($ fp, 4096));
        >>>}
        >>>fclose($fp );
        >>>}
        >>>
        >>>[/color]
        >>Assuming $xtype is defined correctly somewhere, I reckon IE is choking on
        >>the Content-Transfer-Encoding header. This is a MIME header -- it doesn't
        >>belong in an HTTP response at all. Go and read RFC 2616 some time.
        >>
        >>You don't need to cast $filesize to a string -- PHP will do this for you
        >>anyway. And while you're at it, I suggest you replace the while() loop
        >>with
        >>a readfile() statement instead (<http://uk.php.net/readfile>).
        >>
        >>Oh, by the way, filesize() is another useful function. That way you can
        >>get
        >>rid of the fopen and fclose instructions altogether.
        >>
        >>[/color][/color]
        Here's what works for me. I haven't tested your case with IE but you
        might give it a try.

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

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

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

        readfile($file) ;

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

        Integrity is obvious.
        The lack of it is common.
        *************** **************

        Comment

        • C16

          #5
          Re: Php Download script problem!

          Many thanks Chuck that did the trick. For interest the header I have that
          works is now:

          header("Cache-Control: post-check=0, pre-check=0");
          header("Expires : 0");
          header('Content-Description: File Transfer');
          header("Content-Type: " . $xtype);
          header("Content-Length: " . $filesize);
          header('Content-Disposition: inline; filename=' . basename($filen ame));
          [color=blue]
          > Here's what works for me. I haven't tested your case with IE but you might
          > give it a try.
          >
          > header('Content-Description: File Transfer');
          > header('Content-Type: application/pdf');
          > header('Content-Length: ' . filesize($file) );
          >
          > // to download
          > header('Content-Disposition: attachment; filename=' . basename($file) );
          >
          > // to open in browser
          > //header('Content-Disposition: inline; filename=' . basename($file) );
          >
          > readfile($file) ;
          >
          > --
          > *************** **************
          > Chuck Anderson • Boulder, CO
          > http://www.CycleTourist.com
          > Integrity is obvious.
          > The lack of it is common.
          > *************** **************[/color]


          Comment

          Working...