Computing.Net > Forums > Web Development > error - php file to download pdf.

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

error - php file to download pdf.

Reply to Message Icon

Name: redgirl63
Date: December 18, 2008 at 19:27:43 Pacific
OS: redcat
CPU/Ram: 1
Product: 1 / 1
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: shutat
Date: December 19, 2008 at 14:50:14 Pacific
Reply:

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.


0

Response Number 2
Name: redgirl63
Date: December 19, 2008 at 19:52:34 Pacific
Reply:

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



0

Response Number 3
Name: shutat
Date: December 19, 2008 at 23:41:00 Pacific
Reply:

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


0

Response Number 4
Name: redgirl63
Date: December 20, 2008 at 22:40:01 Pacific
Reply:

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


0

Response Number 5
Name: shutat
Date: December 22, 2008 at 16:47:01 Pacific
Reply:

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.


0

Related Posts

See More



Response Number 6
Name: shutat
Date: December 22, 2008 at 16:49:28 Pacific
Reply:

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


0

Response Number 7
Name: redgirl63
Date: January 6, 2009 at 18:33:29 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: error - php file to download pdf.

PHP File Downloader? www.computing.net/answers/webdevel/php-file-downloader/1934.html

PDF file to be Downloaded www.computing.net/answers/webdevel/pdf-file-to-be-downloaded/405.html

download pdf hyperlink www.computing.net/answers/webdevel/download-pdf-hyperlink/792.html