I'm building a site for a friend where people can download his music. For security and logging purposes, I'm handling all file downloads through a php page and I'm ouputting the file when I'm done checking permissions and logging. Here's the code I'm using.

header('Content-Length: '.$clip->getFilesize());
header('Content-Type: '.$clip->getContentType());
header('Content-Disposition: attachment; filename="'.$clip->getFilename().'"');
@readfile($path);

Internet Explorer doesn't want to be a team player (as usual). Firefox, Opera and SeaMonkey are fine. I get a Save As... dialog, and I can either select Open or Save As..., and the file gets either opened in the appropriate application or saves to my hard drive with the name I output in the header. In IE, when I select "Save As..." it times out and says it can't find the file. It does, however, open the file if I select Open, but in both cases it uses the name of the page (download.php?id=...) instead of the filename I output in the header.

Anyone got some pointers on this issue, besides the obvious IE bashing?

    Not sure if this will make a difference with IE, but you could try changing the content-type to:

    header("Content-Type: application/octet-stream");
    
      NogDog wrote:

      Not sure if this will make a difference with IE, but you could try changing the content-type to:

      header("Content-Type: application/octet-stream");
      

      if that works, maybe i'll look for IE in the UserAgent string and base the Content-Type header on that. I'd still rather get a solution that doesn't require "hacking" for IE (like the CSS nightmares I have in IE), but if no standard exists I'll just go with this. Thanks for your reply.

        I found a solution on the [man]readfile[/man] page. I had to add two more headers before outputting the file, so the final working code is:

        header('Content-Length: '.$clip->getFilesize());
        header('Content-Type: '.$clip->getContentType());
        header('Content-Disposition: attachment; filename="'.$clip->getFilename().'"');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        @readfile($path);

          Thanks for following up with your solution.

            Write a Reply...