Hi There,

I have spent over an hour searching for something that works so I appreciate your patience. BTW the browser I'm using is IE6

Trying to force a download, here is the code that I'm using:

$filename="$DOCUMENT_ROOT/pdf/AP20030817W.pdf";
header("Content-type: application/octet-stream"); 
header("Content-Disposition: attachment; filename=\"AP20030817W.pdf\""); 
header("Content-transfer-encoding: binary\n"); 
header("Content-length: " . filesize($filename) . "\n"); 
fread("$filename");

The problem is that the first dialog message that comes up is "You are downloading the file dl12.php from mysite.com" -- The actual file attachment is a PDF, and as you can see I have named it.

However when I click Save, the name AP20030817W.pdf IS the suggested name, so it still works out OK. However, it's still irritating getting the wrong file name, and a lot of in-the-know people will notice this.

I have looked for OVER AN HOUR trying to overcome this. Can anyone give me a tip?

Thanks,
Sam Fullman

    I'm not sure why, but this works for me:

    header ("Accept-Ranges: bytes");
    header ("Connection: close");
    header ("Content-Length: " . $filesize); 
    header ("Content-Disposition: attachment; filename=\"" . $filename . "\"");
    echo $filedata;
    

      Yes you will get the wrong name in the initial box. This is because the file is your php file at that point. It isn't until the person decides what to do with the attachment that the filename in your headers mean anything.

      I've never tried it but you could attempt to send the file inline instead of attached.

        I've got the right name in the initial box too, by just using the code I posted above.

          Sorry, but in IE6.0 (a good starting place), you do NOT get the right name.

          It'll say:

          You are downloading the file:
          download.php from thissite.com
          Would you like to open the file or save it to your computer?
          [OPEN] [SAVE] [CANCEL] [CANCEL] [MORE INFO]

          Now, IF I choose to save, yes, in the next window (Save As) the 'file name:' suggested is in fact the $filename I'm trying to download, but nevertheless, it would confuse a more savvy web customer who is LOOKING for .pdf in the very first window.

          So, nice try, and I've researched may differnent ways of doing this, but so far no dice.

          Sincerely,
          Sam Fullman

            And I'm sorry as well, but in my IE 6, with the code I posted, I DO get the right name even in the first window. Here is a screen shot of how it looks on my computer, and for good measure - I'll also post the complete code for my file "file_download.php" below:

            $id = $_GET["id"];
            
            $sql = "SELECT filesize, filetype, filename FROM extra_forumthreads WHERE id = $id";
            $rs = mysql_query($sql);
            
            $rows = mysql_fetch_array($rs);
            
            $file = fopen("/Library/Webserver/Documents/extra/forumfiles/" . $id . ".file", "r");
            $filedata = fread($file, $rows["filesize"]);
            fclose($file);
            unset($file);
            
            header ("Accept-Ranges: bytes");
            header ("Connection: close");
            header ("Content-type: " . $rows["filetype"]);
            header ("Content-Length: " . $rows["filesize"]); 
            header ("Content-Disposition: attachment; filename=\"" . $rows["filename"] . "\"");
            echo $filedata;
            

              HI Mungo,

              It looks from your screen shot that you have XP. I have Windows 2000, perhaps that's the issue. Anyway, since I'm not connecting to a database, here is the code I used, but it's the same as yours in syntax:

              
              $fileName='temp.doc';
              $fileType='application/msword';
              #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
              
              $fp = fopen($fileName, "r"); 
              $filedata = fread($fp, filesize($fileName)); 
              fclose($fp); 
              unset($fp); 
              
              header ("Accept-Ranges: bytes"); 
              header ("Connection: close"); 
              header ("Content-type: $fileType"); 
              header ("Content-Length: ". filesize($fileName));  
              header ("Content-Disposition: attachment; filename=\"$fileName\""); echo $filedata;

              And it still does the same thing. It says 'you are saving the file download.php' It does this with .pdf filetypes as well (which is what I'm really shooting for).

              On this subject of forcing downloads and headers I've seen as many theories as I've seen people, and there doesn't seem to be a definitive guide out there that takes into account all of the factors -- inline vs. attachment, browser types, etc. etc. -- I had the same difficulty in email attachments though I finally mastered that one.

              If the above code works in XP, it's on OS thing, not a browser thing. BTW I have IE version 6.0.2600.

              Thanks again
              Sam

                That could be the thing, the difference lies in the OS. If you want to send me a link to where I could try out your download code, I could let you know what my XP says.

                  Sent you a PM with the link, thanks.

                  Sam

                    Write a Reply...