SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jul 2007
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    exporting/downloading txt file - readfile(); problems - creating line breaks

    Greetings,

    I created a script to export member's items into a tab-delimited txt file. The script works great, it escapes tabs and new lines and creates a perfect export.txt file.

    Problem: I have another link so people can download the export.txt file onto their computers using:

    PHP Code:
        $file "export-files/".$member.".txt";
        
    header('Content-Description: File Transfer');
        
    header('Content-Type: text/plain');
        
    header('Content-Disposition: attachment; filename='.basename($file));
        
    header('Content-Transfer-Encoding: binary');
        
    header('Expires: 0');
        
    header('Cache-Control: must-revalidate');
        
    header('Pragma: public');
        
    header('Content-Length: ' filesize($file));
        
    ob_clean();
        
    flush();
        
    readfile($file);
        exit(); 
    When I download export.txt, it appears to have newly created line-breaks that are not found in the export.txt file on the server. Does anyone know why this happens and how to prevent this?

    Thanks

  2. #2
    SitePoint Addict
    Join Date
    Jul 2007
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems like if a line of about 1024 characters, a line break happens - only on my downloaded file though...

  3. #3
    SitePoint Addict
    Join Date
    Jul 2007
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nevermind, figured it out. 1024 characters was the maximum length of a line in a txt file viewed on Notepad. It looked fine in the browser since it's not Notepad.

    In addition, "\n" does not produce a new line in a text file, you must put in "\r\n" when writing a txt file to recognize it as linebreak. This fixed the export, file did not re-import due to a single tab at the end of the last row.

    So yeah, two characters "\r" and a TAB gave me an 8 hour awful waffle... At least I learned something about a topic I've never done before!

    Thanks for looking!


Bookmarks

Posting Permissions

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