Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 messagebelow 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();
?>

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.
<?phpif(isset($_GET["fn"])) { die($_GET["fn"]); }
header("location: " . $_SERVER["PHP_SELF"] . "?fn=hillview.pdf");
?>Hope that helps.

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();
?>

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";
}
?>

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

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.

oops; I didn't notice it until after I posted. border="0" goes within the img tag and not the anchor tag.

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...

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |