Setting content-length on an exe download

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oz1cz
Forum Newbie
Posts: 5
Joined: Mon Nov 03, 2008 3:34 am

Setting content-length on an exe download

Post 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
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Re: Setting content-length on an exe download

Post 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>
oz1cz
Forum Newbie
Posts: 5
Joined: Mon Nov 03, 2008 3:34 am

Re: Setting content-length on an exe download

Post 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
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Re: Setting content-length on an exe download

Post 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.
oz1cz
Forum Newbie
Posts: 5
Joined: Mon Nov 03, 2008 3:34 am

Re: Setting content-length on an exe download

Post by oz1cz »

Perfect, Jason. That works exactly as it should. Thank you very much!

--
Claus
Post Reply