...

View Full Version : Downloading text file using PHP



starvos
06-15-2010, 03:48 PM
I have some text files on the server in table format(columns & rows) and I used the following script to download it to my pc:


$path = $_SERVER['DOCUMENT_ROOT']."/lib/reports/"; // change the path to fit your websites document structure
$fullPath = $path.$_REQUEST['dlfile'];
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
header("Content-length: $fsize");
while(!feof($fd)) {
$buffer = fread($fd, $fsize);
echo $buffer;
}
}
fclose ($fd);
exit;


However, when I opened the downloaded text file, it gives me a long string of data instead of the columns & rows format. Can someone advise me how to rectify this?

abduraooft
06-15-2010, 04:10 PM
However, when I opened the downloaded text file, it gives me a long string of data instead of the columns & rows format. I don't understand that part. Anyway, are you echoing anything before the above code?

starvos
06-16-2010, 09:01 AM
I don't understand that part. Anyway, are you echoing anything before the above code?

What I meant was the text file stored in server is in this column/row format :
eg.
Name Age Gender
John 40 Male
Susan 25 Female

Using the script, the downloaded text file reads:

Name Age GenderJohn 40 MaleSusan 25 Female

How can I rectify this?

xGIHavoc
06-16-2010, 09:40 AM
It may be a difference in the way the text is encoded on the server versus your own filesystem. I could be wrong but try:
header("Content-Transfer-Encoding: binary");

abduraooft
06-16-2010, 11:39 AM
Have you opened it in notepad++ ?

starvos
06-16-2010, 01:55 PM
Have you opened it in notepad++ ?

It works fine in notepad++ but not in notepad, why is that so?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum