Page 1 of 1

Setting content-length on an exe download

Posted: Mon Jan 04, 2010 9:37 am
by oz1cz
I'm trying to create a web page which allows users to download a Windows EXE file.

My first attempt was to include this simple hyperlink in the page:

Code: Select all

<a href="xyzzy.exe">download</a>
This works OK, sort of...

The web server (an IIS server over which I have no control beyond what PHP gives me), does not include a Content-length header in the file transfer, which means that the browser does not no how long the download will be.

Can I somehow use PHP (or some other technique) to force the inclusion of a Content-length on the EXE file transfer? Or is there, perhaps, a more correct way to do what I want?

--
Claus

Re: Setting content-length on an exe download

Posted: Mon Jan 04, 2010 9:44 am
by jason
Pretty sure you can just use filesize() to get the size of the file, and pass that as the value of Content-Length. To set Content-Length, you'd want to use the header() function. Basically:

Code: Select all

 
header("Content-Length: ".filesize($file));
 
You'd set something like that up on a page like download.php, and simply pass the file as a variable, something like:

Code: Select all

download.php?fileToDownload=zomgwtfbbq.exe
Of course, you'd want to do error/security checking etc.

Edit: Sorry, to use your example, you'd use:

Code: Select all

<a href="download.php?fileToDownload=xyzzy.exe">download</a>

Re: Setting content-length on an exe download

Posted: Mon Jan 04, 2010 9:52 am
by oz1cz
Hi Jason,

The problem is that if I do as you suggest:

Code: Select all

download.php?fileToDownload=zomgwtfbbq.exe
the browser will try to save the file under the name download.php rather than zomgwtfbbq.exe.

--
Claus

Re: Setting content-length on an exe download

Posted: Mon Jan 04, 2010 10:05 am
by jason
Sorry.

You'd also use this header as well:

Code: Select all

 
header('Content-Disposition: attachment; filename="NameOfTheFile.pdf"');
 
http://php.net/manual/en/function.header.php

Obviously, you can change NameOfTheFile.pdf to whatever you want the name to be.

Re: Setting content-length on an exe download

Posted: Mon Jan 04, 2010 10:15 am
by oz1cz
Perfect, Jason. That works exactly as it should. Thank you very much!

--
Claus