Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts

    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 

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    You're viewing the downloaded text in which editor?

  • #3
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    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');

  • #5
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    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.

  • #7
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •