Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-02-2011, 10:30 PM   PM User | #1
mathceleb
Regular Coder

 
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
mathceleb is an unknown quantity at this point
Dat file download adding trailing spaces

If I do this code with a .dat file, it inserts spaces at the end of the file. Do I need something special for the header for .dat files?

PHP Code:
// prompt save/download
$output_file_name="information.dat";
$file_string="hello world";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: private");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$output_file_name");
header("Accept-Ranges: bytes");
echo 
$file_string;
// end prompt save/download 
mathceleb is offline   Reply With Quote
Old 06-02-2011, 10:42 PM   PM User | #2
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
You're viewing the downloaded text in which editor?
MattF is offline   Reply With Quote
Old 06-02-2011, 10:51 PM   PM User | #3
mathceleb
Regular Coder

 
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
mathceleb is an unknown quantity at this point
Quote:
Originally Posted by MattF View Post
You're viewing the downloaded text in which editor?
Both PSPad and Notepad.

I even ran a check to measure the length of the string going out immediately before the download to make sure it matched what it should be.
mathceleb is offline   Reply With Quote
Old 06-02-2011, 11:07 PM   PM User | #4
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
First thing I'd try, if you have acces to a *nix box, is checking the file in vi, to make sure the editors aren't adding any erroneous padding. The other possibility is encoding. Might be worth trying a binary type for the download:

Code:
header('Content-Transfer-Encoding: binary');
MattF is offline   Reply With Quote
Old 06-03-2011, 03:23 PM   PM User | #5
mathceleb
Regular Coder

 
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
mathceleb is an unknown quantity at this point
I tried that as well. There is 23 extra spaces being tacked on to the last record. If I loop through 50 of those records above, 49 of them are great, and the 50th record has that strange space padding at the end.
mathceleb is offline   Reply With Quote
Old 06-03-2011, 04:26 PM   PM User | #6
mathceleb
Regular Coder

 
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
mathceleb is an unknown quantity at this point
I even tried this:

PHP Code:
    $xtot=strlen($file_string);
    
    for (
$x=$xtot;$x>=0;$x--){
    
$cur=substr($file_string,$x,1);
    
$tout=ord($cur);
       
// eliminate spaces, nulls, carriage returns, and line breaks
       
if ($tout!=&& $tout!=10 && $tout!=13 && $tout!=32){
         
$file_string=substr($file_string,0,$x+1);
         break;
       } 
    } 
It picks up the right string, but saving down, it adds spaces. I'm viewing this in Notepad.

Last edited by mathceleb; 06-03-2011 at 04:29 PM..
mathceleb is offline   Reply With Quote
Old 06-03-2011, 07:08 PM   PM User | #7
mathceleb
Regular Coder

 
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
mathceleb is an unknown quantity at this point
This worked below. Not ideal, but it gets it done quickly. Basically, pull the contents back into php, chop it, and then send it back down to the same file.

PHP Code:
$fname="information.dat";
$tempfile=file_get_contents($fname);
$tempfile=chop($tempfile)."\r\n";

$fh fopen($fname'w') or die("Cannot open file");
fwrite($fh$tempfile);
fclose($fh); 
Note: This only works if you write the file and don't prompt the save/open menu.

Last edited by mathceleb; 06-03-2011 at 07:23 PM..
mathceleb is offline   Reply With Quote
Reply

Bookmarks

Tags
dat files, download

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:29 AM.


Advertisement
Log in to turn off these ads.