Page 1 of 1

Downloading a PDF sile from server

Posted: Mon Oct 29, 2007 10:38 am
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]

Posted: Mon Oct 29, 2007 11:01 am
by feyd
Why is substr() being used to retrieve the filename? Have you tried using readfile()? Are you sure $file is set correctly?

Posted: Mon Oct 29, 2007 11:17 am
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

Posted: Mon Oct 29, 2007 11:24 am
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

Posted: Tue Nov 20, 2007 10:35 am
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!!!!

Posted: Wed Nov 21, 2007 1:53 am
by feyd
Have you compared the original file and the downloaded version (binarily)?

Posted: Wed Nov 21, 2007 6:10 am
by moeamla
Yes I did ... it has the same size and content but I cant view it on a pdf ... HELP PLEASE.....

Posted: Wed Nov 21, 2007 9:48 am
by feyd
Post links to both, please, so we can compare them.

Posted: Wed Nov 21, 2007 11:54 am
by moeamla
I'm sorry, I don't want to sound dumb. But how can I do That !!???

Posted: Wed Nov 21, 2007 12:01 pm
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.

Posted: Wed Nov 21, 2007 12:49 pm
by moeamla
Public server?? Like what ??

Posted: Wed Nov 21, 2007 1:34 pm
by feyd
…like your own web server.

Posted: Wed Nov 21, 2007 2:16 pm
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