Forum Moderators: coopster

Message Too Old, No Replies

File name of download not being set

         

Xaul

10:13 am on Sep 29, 2005 (gmt 0)



I've set up a script to serve files from a directory outside of my www directory. It's working fine for the most part, but it does seem to have one problem. When you try to download a .dvf file, it tries to give me a file named download.php, which is the script being executed at the time. Yet, if i try it with other file types, such as mp3 or jpg, it works fine (it names the file as it should be named). Here's my relevant code -


$fileToOpen = "/home/pathtofile/" . $result['id'] . "." . $result['filetype'];
$handle = fopen($fileToOpen, "rb");
$contents = fread($handle, filesize($fileToOpen));
fclose($handle);
header("Pragma: public");
header("Expires: 0");
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0', false);
header("Content-length: " . $result['filesize']);
header("Content-Disposition: attachment; filename=\"" . $result['filename'] . "\"");
header('Content-Transfer-Encoding: binary');
print $contents;

Yes, I realise that I'm missing the Content-Type header, but that's for a reason. Some of the files being downloaded don't even seem to have a mime type associated with them (such as the dvf file I mentioned before). I don't really care if my browser thinks the file is a htm file, just naming it correctly will be sufficient. And no, putting that header in doesn't seem to make any different (though I must admit, I have no idea what to set the content type to for dvf files).

coopster

1:25 pm on Sep 29, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, Xaul.

I'm guessing the browser you are using is MSIE, correct? Is your form using an anchor tag with an href link, or does the user select the filename from a list and submit the request?

Xaul

1:40 pm on Sep 29, 2005 (gmt 0)



I'm a Firefox user, but the result is the same in IE and Firefox.

The user retrieves the file through a normal anchor tag. So, it'll be something like

<a href="?file=38">Download</a>

and then at the top of the page, i've got

if (isset($_GET['file'])) {
//do stuff that i mentioned before
}

coopster

2:55 pm on Sep 29, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



But you are naming the filetype ...

$result['filename']

... not 'file'. Have you confirmed what is in $result['filename']?

Xaul

4:26 pm on Sep 29, 2005 (gmt 0)



Yes, the contents of $result['filename'] is correct - it is just meant to contain the filename, to be used in the Content-Disposition header. All that is specified in that header is that it is an attachment, and the filename itself. As you can see from the rest of the code, the real file is read into the variable $contents, and then printed once all the appropriate headers have been sent. As I've said before, my script works perfectly on various other files, such as mp3's and jpg's. The file downloaded is named correctly and everything. It's just that with dvf files that the problem occurs, and I end up receiving a file with the same name as my script.