Results 1 to 3 of 3

Thread: UTF-8 BOM added to downloaded files

  1. #1
    evalutioner is offline Junior Member
    Join Date
    Apr 2010
    Posts
    2

    Default UTF-8 BOM added to downloaded files

    Hi everybody!

    I've got the following problem: when I download a note attachment, I obtain a file of right size, but it's first 3 bytes is UTF-8 BOM (EF BB BF) and last 3 bytes are truncated. Everywhere charsets are set to 'UTF-8', not 'UTF-8 BOM'. The problem is reproduced in every browser.

    My config:
    Sugar 5.5.1 + IIS + SQL Express (from stack installer).

    Files in %sugar%\cache\upload are ok.
    When I open note -> click file and save it -> I obtain corrupted attachment.

    further research has shown that the problem could be with php: download.php contains code that manages attachment download and somewhere here UTF-8 BOM is added (checked everywhere - UTF-8 set by default).

    header("Pragma: public");
    header("Cache-Control: maxage=1, post-check=0, pre-check=0");
    header("Content-type: application/force-download");
    header("Content-Length: " . filesize($local_location));
    header("Content-disposition: attachment; filename=\"".$name."\";");
    header("Expires: 0");
    set_time_limit(0);

    @ob_end_clean();
    ob_start();

    echo file_get_contents($download_location0);
    @ob_end_flush();

    Can you advice me where to disable insertion of this ^%#king BOM?

    rgds,
    evalutioner
    Last edited by evalutioner; 2010-04-23 at 12:00 PM. Reason: adding some details

  2. #2
    freedbill is offline Member
    Join Date
    Feb 2009
    Posts
    10

    Default Re: UTF-8 BOM added to downloaded files

    Here are two additional posts with the same problem.
    http://www.sugarcrm.com/forums/showt...264#post212264

    and

    http://www.sugarcrm.com/forums/showt...263#post212263

    I posted the first link (JackofAll) almost one year ago. I'm not sure where the solution lies, however, at least I'm seeing others with the same problem.

    My environment is My environment: Linux 2.6.9 Fedora Core 7; PHP 5.2.6; MYSQL 5.0; APACHE 2.2; SUGARCRM 5.2.0.b (on one installation) and 5.5.1 at a second installation.

    Hope this helps bring great minds together and gives rise to a solution.

    Bill

  3. #3
    zullek is offline Junior Member
    Join Date
    May 2010
    Posts
    2

    Default Re: UTF-8 BOM added to downloaded files

    This problem occures, because one of SugarCRM's system file is saved in UTF-8 + BOM encoding (usually it's config.php, but can be any other file, that was edited by administrator of portal).

    This small php script will help you to find files with BOM:

    PHP Code:
    <?php

    $bad_files 
    = array();
    $directory ".";        // Current directory
    $fix_file FALSE;    // Change to TRUE, if you want to automatically fix files with BOM mark
    $recursive_check TRUE;

    scanDirectory($directory$bad_files);

    if (
    count($bad_files) > 0)
    {
        echo 
    "<br><br><br>Found the following files with a bad BOM:<br>";
        
    printResults($bad_files);
    }
    else
        echo 
    "No Errors Found";


    function 
    scanDirectory($directory, &$bad_files)
    {
        global 
    $recursive_check;
        
    $skip_directories = array(".""..""cache");

        if (
    $handle opendir($directory))
        {
            echo 
    "Checking Directory: $directory<br>";

            while (
    false !== ($file readdir($handle)))
            {
                
    $fullFile $directory DIRECTORY_SEPARATOR $file;

                
    // Recursive Call for sub-directories.
                
    if (is_dir($fullFile) && $recursive_check)
                {
                    if (
    in_array($file$skip_directories))
                        Continue;

                    
    scanDirectory($fullFile$bad_files);
                }

                if (
    checkFile($fullFile$file))
                    
    $bad_files[] = $fullFile;
            }

            
    closedir($handle);
        }
    }

    function 
    checkFile($file$fileOnly)
    {
        global 
    $fix_file;

        if (!
    is_file($file))
            return;

        if (
    substr($fileOnly, -4) == ".log")
            return;

        echo 
    "&nbsp;&nbsp;$fileOnly";

        
    $isBad FALSE;
        
    $file_contents file_get_contents($file"+r");

        
    $hex bin2hex($file_contents);
        
    $first_token substr($hex06);

        if (
    $first_token == 'efbbbf')
        {
            
    $isBad TRUE;
            echo 
    "&nbsp;...FOUND ERROR<br>";
            
            if (
    $fix_file)
                
    fixFile($hex,$file);
        }
        else
            echo 
    "&nbsp;...OK<br>";

        return 
    $isBad;
    }


    function 
    printResults($results)
    {
        foreach (
    $results as $fileName)
            echo 
    "$fileName<br>";
    }


    function 
    fixFile($hex$file)
    {
        
    $fp fopen($file'w');
        
    $good substr($hex6);
        
    $good_string pack('H*'$good);
        
    fwrite($fp$good_string);
        
    fclose($fp);
    }

    ?>
    Just create some php file in root directory of your SugarCRM portal (for example, fixbom.php). Paste there provided code and then open it thru browser.

    When the script will finish it's work - it will show you list of files with BOM mark (at the end of page).
    Then you can fix those files manually (change encoding to UTF-8 instead of UTF-8 + BOM) or automatically - replacing $fix_file with TRUE in the beginning of script.

    P.S.
    Source code were taken and slightly modified from Andreas Sandberg (http://developers.sugarcrm.com/wordp...n-eval-errors/)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Emails are downloaded again and again!
    By khalidh in forum General Discussion
    Replies: 0
    Last Post: 2008-10-14, 10:14 PM
  2. 5.1 zip downloaded.
    By Carlson in forum Help
    Replies: 2
    Last Post: 2008-10-02, 05:32 PM
  3. implement a downloaded hook
    By michamamo2 in forum Help
    Replies: 0
    Last Post: 2008-09-11, 10:05 AM
  4. Email downloaded but doesn't appear
    By monsta in forum Help
    Replies: 0
    Last Post: 2007-08-20, 11:17 AM
  5. No more emails downloaded in 4.5.0c
    By fvarenne2 in forum Help
    Replies: 3
    Last Post: 2006-10-05, 04:10 PM

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
  •