SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    File download is not working - showing non readable text or content on same page

    Hi,

    I have used below file download php script which works fine on my PC. But when I published this code to server, it throws two errors

    1. Warning: Cannot modify header information - headers already sent by... To avoid this warning I have used ob_start();... which helped to remove these warnings but major problem is
    2. when clicked download button, it shows text which is not in readable format.

    Code used
    PHP Code:
            ob_start();
            
            
    $file "upload/".$filecode;
            
    $actualfilename $filecode;
            
            if (
    file_exists($file))
            {                
                
    $INC_DIR $_SERVER["DOCUMENT_ROOT"]. "/inc/"
                
                
    $con $db->fnInitConn();
                
                
    header('Content-Description: File Transfer');
                
    //Below line is replace by force-download because this was not working when downloaded from web server
                
                
    header('Content-Type: application/x-msdownload');
                
    //header("Content-Type: application/force-download");
                //header("Content-Type: application/zip");
                
                
    header('Content-Disposition: attachment; filename='.$actualfilename);            

                
    //header('Content-Transfer-Encoding: binary');
                
    header('Expires: 0');
                
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                
                
    header('Pragma: public');
                
    header('Content-Length: ' filesize($file));
                
                
    ob_clean();
                
    flush();
                
                
    readfile($file);
                
                
    //$handle=fopen($file,"r") or exit("Unable to open file!"); //This code is not tested
                
                
    exit;
            } 
    I have kept some commented code just to show that I have tried multiple options. It's not just that an executable or zip file not able to download but other files like TXT or EXCEL are showing same non readable text.

    Please help!
    Last edited by cpradio; May 24, 2013 at 06:43. Reason: Added php tags

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There must be no output whatsoever before the header, not even a blank line or any html. If your <?php tag is not in position 1 on line 1 you'll get the error. Any output causes headers to be sent.

    You don't need ob_start();

    BTW, the order of your logic is not good.

    PHP Code:
    //Do all your error checking first, 
    //then get the file contents
    //then output headers
    //then output file
    //then kill the script [exit;]; 
    Denny Schlesinger
    web services

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by captainccs View Post
    You don't need ob_start();
    More importantly, you cant have ob_clean();

    Your current script does this:
    Start an output buffer.
    Check if the file exists.
    Start a database connection for no discernable reason, as it's never used.
    Put all your content headers into the output buffer.
    Throw away the output buffer (ob_clean)
    Output the (now empty) output buffer.
    Read file to output buffer.
    Die; Garbage cleanup closes database connection and dumps buffer to browser.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


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
  •