Page 1 of 1

Download files, content-type

Posted: Thu Feb 20, 2003 3:48 pm
by dstefani
Hello,
I am trying to allow users to create a text file (not using a txt extention) from a MySQL query, file is created and then I want to open the save file as dialog for them to download the file, then I unlink() the file from the directory. By using a slight twist on the manuals suggested way of doing this that I found on the PHPfaq page, I have the functionality I want, but only in IE6 and NS7 (win). If I try it with IE5.5 - win, it offers the file loaded in the window, but if I choose to open file from location, it opens the created text file, if I try to save it locally, it saves the current page open in the browser.

I looked at the download lib of the phpMyAdmin, but it's very complicated and I need to get this going ASAP. Has any of you folks ever built something like this before, or no of a place where I can find the requirments for the different browsers.

Thanks,

- D

Here is what I'm using now:

Code: Select all

$save_as = $filename . "." . $file_type;

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$save_as");
if (strpos($save_as,'\'') !== false 
  or strpos($save_as,'/') !== false
  or strpos($save_as,':') !== false) die('Not current directory');

if (!readfile($save_as));

Posted: Fri Feb 21, 2003 9:47 am
by dstefani
Here's one fix I found to make this code work for IE5.5.1
Change the line:

Code: Select all

header("Content-Disposition: attachment; filename=$save_as");
//to
header("Content-Disposition: filename=$save_as");
I guess IE5 can't used the 'attachment' so it reads the first line of the file and tries to open the proper application. If the 'save file' is selected it works fine.

It does do a bit of weirdness in NS6-7, but the original code worked for that, so bit of browser checking and conditions should make it work for these.

What's needed is a object for handling all of the known bugs.
Hmmm...

- D