[SOLVED] Force download files

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
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

[SOLVED] Force download files

Post by tristanlee85 »

I've created a downloads section on my forum and the files that are being downloaded are.msq files. Right now, if a user clicks the link to download the file, it opens the file as a text file within the browser. Can I use header() to force the file to b downloaded?
Last edited by tristanlee85 on Thu Sep 28, 2006 1:06 am, edited 3 times in total.
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Nevermind. I just had to comment something out.

Doesn't work

Code: Select all

if (isset($_GET['download']))
{
        $filename = $_GET['download'];
        header("Content-Type: ".get_mimetype($filename));
        header("Content-Length: ".filesize($path . $filename));
        header("Content-Disposition: attachment; filename=$filename");
        readfile($path . $filename);

}
Does work

Code: Select all

if (isset($_GET['download']))
{
        $filename = $_GET['download'];
        header("Content-Length: ".filesize($path . $filename));
        header("Content-Disposition: attachment; filename=$filename");
        readfile($path . $filename);
}
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Alright. I guess none of them work. When I click the download link for my file, I'll open it to check the source of the file and the whole source is the entire web page source. Why are my files getting saved as the web page instead of true file?
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

What I have works. I had a quote in the wrong spot for my link and it was trying to open the file http://...file.msq%20target=.
Post Reply