RE: [PHP] Force Download Link Files?

From: Date: Mon, 21 Apr 2003 15:32:36 +0000
Subject: RE: [PHP] Force Download Link Files?
Groups: php.general 
Request: Send a blank email to php-general+get-144649@lists.php.net to get a copy of this message
thanks a lot! it worked but I have another question my code: $fp = fopen($filen, "r"); $string = fread($fp, filesize($filen)); $mime_type = (PMA_USR_BROWSER_AGENT == 'IE' || PMA_USR_BROWSER_AGENT == 'OPERA') ? 'application/octetstream' : 'application/octet-stream'; header('Content-Type: ' . $mime_type); if (PMA_USR_BROWSER_AGENT == 'IE') { header('Content-Disposition: inline; filename="' . $filename . '.' . $ext . '"'); header("Content-Transfer-Encoding: binary"); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); print $string; } else { header('Content-Disposition: attachment; filename="' . $filename . '.' . $ext . '"'); header("Content-Transfer-Encoding: binary"); header('Expires: 0'); header('Pragma: public'); print $string; } THis works, and all I have to do is put a link to this file and pass in the variables. I used target="_blank" to link to this link, and when the dialog comes up and you press save, the new window stays. However if you press "open" it would download the file to the temporary directory and then close the new window and opens the file (only for jpg and gif files) in the original window. Can I make it "open" in the new window that I opened other than displaying it on the original window? THanks. Kelvin -----Original Message----- From: Kevin Stone [mailto:kevin@helpelf.com] Sent: Thursday, April 17, 2003 5:04 PM To: php-general@lists.php.net Subject: Re: [PHP] Force Download Link Files? ----- Original Message ----- From: "Poon, Kelvin (Infomart)" <kpoon@infomart.ca> To: <php-general@lists.php.net> Cc: <spideyyy@hotmail.com> Sent: Thursday, April 17, 2003 2:26 PM Subject: [PHP] Force Download Link Files? > Hi all, > > I have some link to some files (.jpg, .doc, .xls) and normally when you > "left" click on it, it would open the file. And if you want to download the > file you would have to "right" click and "save as.." I want to enable my > user to be able to download these files by just "left" clicking on the link. > > I have tried many things, I try linking it to a new php page and sent in > some headers, my code: > > <? > header("Content-type: application/octet-stream"); > header("Content-Disposition: filename='ABC.gif'"); > ?> > > > However, it didn't work. Then someone told me to try this, > > <? > header("Content-type: application/octet-stream"); > header("Content-Disposition: filename='ABC.gif'"); > print "bal bla bla"; > ?> > > I did, this time it lets me save the ABC.gif (an dialogue box comes up and > ask me to open or save), and after saving it, when I try to open my file, I > see a big red X (the file is probably empty). I don't know why. Can anyone > please help me? THanks a lot > > Kelvin I believe the example was meant to show you that you need to send data to the output buffer (ie. print it to the screen) after you set the headers.. header("Content-type: application/octet-stream"); // invoke file save dialog header("Content-Disposition: filename='ABC.gif'"); // set default filename in dialog readfile("image.gif"); // send file to output buffer This is the bare minimum. Since the browser is responsible for header parsing and actions, and there are different versions of headers, there are a number of pitfalls to watch out for that you will need to research in order to build a competent download script. - Kevin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.general (#144649) next »