error - php file to download pdf.

December 18, 2008 at 19:27:43
Specs: redcat, 1
hi

I am new to php.

I found a script on this site using a php file to download pdf.


I keep getting Bad access attempt message

below is the code.

I have turn php safe mode off. Permissions to 755 on the php file.

the download.php file and the pdf file are in the smae directory


what am I doing wrong

<?php
$filename = $_GET['hillview.pdf'];
if( ! is_file($filename) || $filename[0] == '.' || $filename[0] == '/' )
die("Bad access attempt.\n");
$file = explode('.', $filename);
if($file[1] != 'pdf')
die('Could not download selected file.');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($filename).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>


See More: error - php file to download pdf.

Report •


#1
December 19, 2008 at 14:50:14
if(!is_filename($filename) || ...) {
die("Bad access attempt");
}

The *apparent* reason your code is failing is each line is being processed in order, so when it gets to die, the script terminates. Try adding brackets as above.

How exactly is the $_GET uri used? If it's something like ?hillview.pdf then I'm thinking $_GET is using hillview.pdf as the key, but its value is null. Try something like below as a quick check to see whether or not you get any output.

<?php

if(isset($_GET["fn"])) { die($_GET["fn"]); }

header("location: " . $_SERVER["PHP_SELF"] . "?fn=hillview.pdf");
?>

Hope that helps.


Report •

#2
December 19, 2008 at 19:52:34
this time I get

'Could not download selected file.'

the code I used was:


<?php
$filename = $_GET['hillview.pdf'];
if(isset($_GET["fn"])) { die($_GET["fn"]); }
$file = explode('.', $filename);
if($file[1] != 'pdf')
die('Could not download selected file.');
header("location: " . $_SERVER["PHP_SELF"] . "?fn=hillview.pdf");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($filename).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>



Report •

#3
December 19, 2008 at 23:41:00
Hopefully, this will work out for you.

<?php

if(isset($_GET["fn"]) && !empty($_GET["fn"])) {

if(!is_file($_GET["fn"])) { die("Bad access attempt"); }

$filename = $_GET["fn"];
$file = explode('.', $filename);

if($file[1] !== "pdf") { die('Could not download selected file.'); }

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($filename).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile($filename);

} else {

echo "<a href=\"" . $_SERVER["PHP_SELF"] . "?fn=hillview.pdf\">download</a> pdf";
}
?>


Report •

Related Solutions

#4
December 20, 2008 at 22:40:01
thanks

that worked.

if I wanted to use a graphic button instead of a text link how would I do this?

I have tried inserting the code


Report •

#5
December 22, 2008 at 16:47:01
No worries. :)

echo "<a href=\"" . $_SERVER["PHP_SELF"] . "?fn=hillview.pdf\" title=\" mouse hover message \" border=\"0\"><img src=\"img_path/img_file\" alt=\"file description\"></img></a>";

title is a tool text tip type of deal and can be whatever you like or omitted altogether if you don't need it.

alt is a text version of what someone would see if the image was broken, missing, etc.

Hope that helps.


Report •

#6
December 22, 2008 at 16:49:28
oops; I didn't notice it until after I posted. border="0" goes within the img tag and not the anchor tag.

Report •

#7
January 6, 2009 at 18:33:29
thats fine - I am experienced in ghtml

I do have another problem with this though.

the pho works fine but if you choose 'open' option from the open/save popup dialogue box after the pdf has finished loading an error message comes up stating the file is damaged.

I have gone to the pdf URL directly and it works fine loading into a browser.

Do you know why this is happening?


please go to http://www.meares.com.au/brochure/h...


Report •


Ask Question