Automatically downloading a file

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
SBukoski
Forum Contributor
Posts: 128
Joined: Wed May 21, 2003 10:39 pm
Location: Worcester, MA

Automatically downloading a file

Post by SBukoski »

I have some code which creates a text file on the server. How do I make it so the download popup window appears for downloading this file. Having it just as a link will open it in the browser, and I don't want my users to have to right click and "save as" to get it.

Any ideas?
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

I believe that's all totally dependent on the MIME types set up on the server. So, you could de-register the MIME for .txt files, and then the site will no longer attempt to display them, but this might have unintended consequences elsewhere. And if you're using shared hosting, this obviously isn't an option.

A better option might be to give your text files a different extension (unrecognized by the server, unused elsewhere). Of course, that just moves the problem client-side, because then your users have to know how to open them.
SBukoski
Forum Contributor
Posts: 128
Joined: Wed May 21, 2003 10:39 pm
Location: Worcester, MA

Post by SBukoski »

I've tried several different file extensions. It always seems to open it in the browser no matter the file extension. I'll probably just end up putting instructions up on how to download it by right clicking. It's only a small hassle and shouldn't bother people too much.

But if anybody knows of a way, speak up. :)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

easy, just use this code and change the paths, and the application/pdf MIME to the MIME for text files.

Code: Select all

<?

	// Get the filename from the query string of the file we want to download
	$fileName = $_GET["file"];
	
	// The full path to our downloads directory
	$fileDir = "/full?path/to/download/directory";
	
	// Combine the filename and the path
	$path = "$fileDir$fileName";
	
	
	////////////////////////////////////////
	/* Force browser to download the file */
	////////////////////////////////////////

	global $HTTP_USER_AGENT;
	$file = basename($path);
	$size = filesize($path);
	header("Content-Type: application/octet-stream");
	header("Content-Type: application/pdf");
	header("Content-Length: $size");
           
	// IE5.5 just downloads index.php if we don't do this
	if(preg_match("/MSIE 5.5/", $HTTP_USER_AGENT)) {
		 header("Content-Disposition: filename=$file");
	} else {
		header("Content-Disposition: attachment; filename=$file");
	}
    header("Content-Transfer-Encoding: binary");
    $fh = fopen($path, "r");
    fpassthru($fh);

?>
then do a link like

Code: Select all

&lt;a href="/download_script.php?file=your_textfile.txt"&gt;Click here to download file&lt;/a&gt;
Mark
SBukoski
Forum Contributor
Posts: 128
Joined: Wed May 21, 2003 10:39 pm
Location: Worcester, MA

Post by SBukoski »

Thanks a ton, that did the trick for me!
Post Reply