Downloading a PDF sile from server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
moeamla
Forum Newbie
Posts: 9
Joined: Mon Oct 29, 2007 10:31 am
Location: Greenville,NC

Downloading a PDF sile from server

Post by moeamla »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey all,

Im trying to download and open (an existing pdf file on the server) on the user's pc and I have this code but it is not 

WORKING!! I read a lot and nothing working

Im using PHP5 and apache

here is the code:

Code: Select all

//$file is the path of the file on the server
//$filename is  the file name
//$filesize is size


$filename=substr($file,20);
$filesize=filesize($file);
header("Content-Disposition: attachment; filename=".$filename);
header("Content-type: application/pdf");
header("Content-Length: ".$filesize); 
$data = file_get_contents($file);
echo $data;


NEED HELP!! PLEASE!!


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Why is substr() being used to retrieve the filename? Have you tried using readfile()? Are you sure $file is set correctly?
moeamla
Forum Newbie
Posts: 9
Joined: Mon Oct 29, 2007 10:31 am
Location: Greenville,NC

Post by moeamla »

Yes, the $file is the file path and i used substr() to get the file name
because there are many files.

example :

$file= htm/reports/2007/today.pdf

so I used
substr() to get the name which is today.pdf
User avatar
seppo0010
Forum Commoner
Posts: 47
Joined: Wed Oct 24, 2007 4:13 pm
Location: Buenos Aires, Argentina

Post by seppo0010 »

You can use basename for that... it's more readable and you might be deleting some extra character, so the file can't be found
moeamla
Forum Newbie
Posts: 9
Joined: Mon Oct 29, 2007 10:31 am
Location: Greenville,NC

Post by moeamla »

Thanks but the code is not working ... all it does is open a pdf and an error occurs mentioning that the file is corrupted but I know that the file is not!!!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Have you compared the original file and the downloaded version (binarily)?
moeamla
Forum Newbie
Posts: 9
Joined: Mon Oct 29, 2007 10:31 am
Location: Greenville,NC

Post by moeamla »

Yes I did ... it has the same size and content but I cant view it on a pdf ... HELP PLEASE.....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Post links to both, please, so we can compare them.
moeamla
Forum Newbie
Posts: 9
Joined: Mon Oct 29, 2007 10:31 am
Location: Greenville,NC

Post by moeamla »

I'm sorry, I don't want to sound dumb. But how can I do That !!???
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Post links to the two PDF files?
  1. Upload them to a publicly accessible server.
  2. Copy and paste the URL of each into a post so we can access them.
moeamla
Forum Newbie
Posts: 9
Joined: Mon Oct 29, 2007 10:31 am
Location: Greenville,NC

Post by moeamla »

Public server?? Like what ??
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

…like your own web server.
moeamla
Forum Newbie
Posts: 9
Joined: Mon Oct 29, 2007 10:31 am
Location: Greenville,NC

Post by moeamla »

I cant It is an Intranet !!!!

What I tried is to redirect the user to the file in the folder on the server and it worked.

:o
Post Reply