PDA

View Full Version : Simple Codes For "Click To View" & "Click To Download"


lemonaden
08-23-2006, 10:04 AM
This is similar to the button post below but different.

Heres what i need to do. On my company site, we are making a page so we can have our company "buy list" posted.

The page is in PHP already because we are using a PHP code to password protect it.

Basically, i made a table. The first table is the "product line" which shows each product line name. The second table would be "click to view" and i want to put a little magnifying glass, so when they click the link the PDF will open up in a browser. On the thirs table, i want it to say "click to save" and have a photo of a floppy disc, and when they click it, it will pop up to save instead of having to click "save target as".

Basically, i need to know how to make seperate the clicks from opening in a browser, to saving. Any suggestions?

Gamini
08-23-2006, 10:47 AM
Yes, it's possible in php with certain headers. But your pdf file for the download section will need to go through a php file, if that's ok. Then here's the code


here is a nice force download scirpt

$filename = $_GET['yourfilename'];
$filename = realpath($filename);

$file_extension = strtolower(substr(strrchr($filename,"."),1));

switch ($file_extension) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpe": case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}

if (!file_exists($filename)) {
die("NO FILE HERE");
}

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".@filesize($filename));
set_time_limit(0);
@readfile("$filename") or die("File not found.");


I took that code from php.net comments section. You need to pass the filename as nameofyourphppage.php?yourfilename=filename.pdf

This should then force the download of the file by using headers.

Hope this helps.

lemonaden
08-23-2006, 11:04 AM
Wow, it seems so confusing.

How would i seperate the files i want to auto load from the files that should save as? They are all different file names but all PDF.

_Aerospace_Eng_
08-23-2006, 11:14 AM
You just link to the php page with yourphpfilename.php?yourfilename=yourfile.pdf not really anything confusing about it. You don't really need to know how it works, you just need to know that it does work.

lemonaden
08-23-2006, 11:18 AM
i dont care how it works.. im just not sure exactly what you guys are telling me i should do.


Your telling me to link my PHP page with yourphpfilename.php?yourfilename=yourfile.pdf what does that mean?

<h1>
08-23-2006, 03:56 PM
The $filename = $_GET['yourfilename']; on the first line of the script will retrieve the value of yourfilename from the url.

So if your url is yourphpfilename.php?yourfilename=yourfile.pdf
then it would retrieve the file "yourfile.pdf" and force download form there (if that file exists)

yourphpfilename.php should contain the code Gamini posted.