0

I have been making a program that creates multiple CSV's from another source CSV (encoded in 'SJIS'/SHIFT-JIS). Here's the process in which I am creating them:

  1. Create a string, which will hold the contents of the output CSV's
  2. Fill in said strings with their proper information
  3. Encode the string to UTF-8 from SJIS using mb_convert_encoding() code:
    $contents2 = mb_convert_encoding($contents, "UTF-8", "SJIS");
  4. Create a zip archive using PHP's provided library methods and append the files I desire with their corresponding strings using addFromString() code:
    $zipFileName = "output.zip";

        $zip = new ZipArchive;
        if ($zip->open($zipFileName, ZipArchive::CREATE) === TRUE){
            $zip->addFromString('customer.csv', $contents2);
            ...do for the other files
            $zip->close();
        }
        else{
            echo 'Failed! File not created!';
        }
    
  5. Prompt the user with a dialogue box to save the file in their desired location. code: $zipContents = file_get_contents($zipFileName); header('Content-Type: application/zip'); header("Content-Disposition: attachment; filename=inflow.zip"); header("Pragma: no-cache"); header("Expires: 0"); echo $zipContents;

Now here is my problem: The files that I have created from the zip file are encoded in "UTF-8 without BOM" when I open it in Notepad++. However, I require for these files to just be in plain "UTF-8". A inventory program I am using to upload these files, for reasons beyond me, will not show the proper characters for the CSV's encoded in "UTF-8 without BOM". Once I manually: open the files, re-encode it as "UTF-8", and save them, are the files able to display the correct characters in this inventory program.

I have read a good deal of articles talking about the converse of this problem, where people were seeking to make their UTF-8 files become encoded without BOM. However, my situation is the exact opposite of this. If there's an easy solution in PHP I would more than welcome the help! Thanks for reading!!

user1166155
  • 43
  • 1
  • 7

1 Answers1

-2

Found a possible solution, see this: How can I output a UTF-8 CSV in PHP that Excel will read properly?

Community
  • 1
  • 1
Tyler Scott
  • 1,046
  • 17
  • 33
  • Thanks for the link I am reading it right now, however I need the opposite of this file functionality; Where I add the BOM to files. I have found other links discussing about the same thing you are suggesting, but not the other way around. http://stackoverflow.com/questions/3127436/adding-bom-to-utf-8-files http://stackoverflow.com/questions/2558172/utf-8-bom-signature-in-php-files Here's the supported encoding by PHP; NOTE: there is "UTF-8" but it defaults to "UTF-8 without BOM" http://www.php.net/manual/en/mbstring.supported-encodings.php – user1166155 Jan 24 '12 at 02:41
  • Oh, ok. I'm looking into it. I'll get back to you. – Tyler Scott Jan 24 '12 at 02:46
  • Are you using uft8_encode function? Also have you tried the fputcsv function? – Tyler Scott Jan 24 '12 at 02:49
  • No i am using the mb_convert_encoding() function. Here's the exact line: $contents2 = mb_convert_encoding($contents, "UTF-8", "SJIS"); And yes I know it's in "UTF-8" but it defaults to "UTF-8 without BOM" when I open the CSV in Notepad++. Which causes me problems. – user1166155 Jan 24 '12 at 02:51
  • The string I am creating is already in CSV format, so I did not use fputcsv() either. – user1166155 Jan 24 '12 at 02:52
  • The above link talks about adding the BOM manually and echoing it not in the file but in the html as well. See if that helps at all. – Tyler Scott Jan 24 '12 at 02:58
  • 1
    Thank you so much for that link! They were discussing the exact problem, and I implemented their suggested solution. It worked! Although now I am encountering a new problem, but I think I can figure out a work around. Thanks again Tyler!! – user1166155 Jan 24 '12 at 03:15