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 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2009
    Posts
    31
    Thanks
    4
    Thanked 4 Times in 4 Posts

    Downloading text file using PHP

    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:
    PHP Code:
    $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?

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    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?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Aug 2009
    Posts
    31
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by abduraooft View Post
    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?

  • #4
    New Coder
    Join Date
    Jan 2006
    Posts
    73
    Thanks
    2
    Thanked 3 Times in 3 Posts
    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:
    PHP Code:
    header("Content-Transfer-Encoding: binary"); 

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Have you opened it in notepad++ ?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    New Coder
    Join Date
    Aug 2009
    Posts
    31
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by abduraooft View Post
    Have you opened it in notepad++ ?
    It works fine in notepad++ but not in notepad, why is that so?


  •  

    Posting Permissions

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