PHP Import CSV file encountered garbled problem solving method _php Skills

Source: Internet
Author: User
Tags explode field table locale rtrim
Today is mainly to write a PHP import CSV file method, in fact, a lot of online search. Can be implemented how to import. But I encountered two problems when I import, one is to write code on Windows when the test has garbled problems, and then resolved. The second is to submit to the Linux system when there is garbled. I don't know why it's garbled at first, at first I thought it was the code SVN commit error, in the end I asked a question in one of my group, a friend is doing phpcms, he said he encountered from Windows submitted to Linux at the beginning also always error, Then the reason for the investigation is garbled result. Below cut to the point to see how to solve two problems!

Problem one solves:

PHP read CSV file, in the windows of Chinese read not to the situation, I immediately think of a function mb_convert_encoding (), as follows set $str = Mb_convert_encoding ($str, "UTF-8", " GBK "); then it's OK. Of course you can also use Iconv () to set the following iconv (' GBK ', ' Utf-8//translit//ignore ', $str); These two functions solve the problem of garbled characters on Windows.

Problem two solves:

PHP read CSV file, on Linux in Chinese can not read the situation, Baidu, Google to find solutions

is to add a line of code setlocale (Lc_all, ' zh_cn '); Yes, blind your eyes. As simple as that, if you don't know it, it may take a long time to solve the problem.
PHP setlocale () function explanation
Definitions and usage

The setlocale () function sets the region information (geographic information).

Locale information is the language, currency, time, and other information for a geographic region. The function returns the current locale and false if it fails.
The following is a collection of commonly used regional identities on the data:

Copy Code code as follows:

ZH_CN GB2312
en_US. UTF-8 UTF-8
ZH_TW BIG5
ZH_HK Big5-hkscs
Zh_tw. EUC-TW EUC-TW
Zh_tw. UTF-8 UTF-8
Zh_hk. UTF-8 UTF-8
Zh_cn. GBK GBK

For example
Utf-8: setlocale (Lc_all, ' en_US. utf-8′);
Simplified: setlocale (lc_all, ' zh_cn ');

The reason I tell you setlocale () is because I import a CSV file to the Linux system when the garbled, including the use of mb_convert_encoding () and Iconv () two functions are not to fix the final problem. Finally add this sentence setlocale (lc_all, ' zh_cn '); Add the code to the beginning of the import CSV file, and then I find the data and find that the Fgetcsv () function is sensitive to the locale. For example, LANG is set to en_US. UTF-8, a single byte-encoded file can have a read error, so we need to set it to culture. Special share to everyone.

I also tried to use the following code and failed to do so, these are the settings that generate the header for the CSV file. It may not work for me, but it might be in your place. So I sorted out, as far as possible to help encounter import CSV file garbled peers, because in the case of no way is really too difficult to deal with. We can all try! There is always one that belongs to you.

Copy Code code as follows:

<?php
$csvContent = "Csvzero,csvone,csvtwo,csvthree,csvfour,csvfive";
Header ("Content-type:application/vnd.ms-excel; charset=gb2312 ");
Header ("Pragma:public");
Header ("expires:0");
Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0");
Header ("Content-type:application/force-download");
Header ("Content-type:application/octet-stream");
Header ("Content-type:application/download");
Header ("Content-disposition:attachment;filename=csv data. csv");
Header ("Content-transfer-encoding:binary");
$csvContent = Iconv ("Utf-8", "gb2312", $csvContent);
Echo $csvContent;
Exit
?>

Here's a concrete look at the PHP import CSV file code:

Two functions for a brief introduction,

Mb_detect_encoding () The character encoding detected, or false when the encoding of the specified string cannot be detected.

The Fgetcsv () function reads a row from the file pointer and resolves the CSV field. Like Fgets (), the difference is that the fgetcsv () resolves the read rows and finds the CSV-formatted fields, and then returns an array containing the fields. Fgetcsv () returns FALSE when an error occurs, including when the end of the file is encountered.

Note: from PHP 4.3.5, the Fgetcsv () operation is binary safe.

Note: The empty row in the CSV file is returned as an array containing a single null field and is not treated as an error.

Note: This function is sensitive to locale settings. For example, LANG is set to en_US. UTF-8, a single byte-encoded file can have a read error.

Note: You can activate the auto_detect_line_endings run-time configuration option if you encounter a line terminator that PHP does not recognize the Macintosh file while reading the file.
Copy Code code as follows:

<?php
SetLocale (Lc_all, ' zh_cn '); Set up regional information (geographic information)
$file = $_files[' FILES '];
$file _type = substr (Strstr ($file [' name '], '. '), 1);
if ($file _type!= ' csv ') {
echo "<script type=\" text/javascript\ >alert (\ "file format error, please upload!\"); </script> ";
Exit
}
$handle = fopen ($file [' Tmp_name '], "R");
$file _encoding = mb_detect_encoding ($handle);
if ($file _encoding!= ' ASCII ') {
echo "<script type=\" text/javascript\ >alert (\ "File encoding error, please upload!\"); </script> ";
Exit
}
$row = 0;
$str = "";
$sy = "";
while ($data = Fgetcsv ($handle, 1000, ', ')) {
$row + +;
if ($row = = 0)
Continue
$num = count ($data);
for ($i =0; $i < $num; $i + +) {
$str = (string) $data [$i]. ' | ';
$str = mb_convert_encoding ($str, "UTF-8", "GBK"); Known source is GBK, converted to Utf-8
$sy. = $str; What I'm doing here is more complicated, with ' | ' Use the contents of the CSV file with ' | ' All together, because I am importing merchandise information, according to user needs
The data to be imported defines which data needs to be imported.
}
}
if ($sy) {$sy = RTrim ($sy, ' | ');}
$arr = Explode (' | ', $sy);
$key = Array_slice ($arr, 0, $num); This array is the title of the CSV file, which is the product ID, title, selling point and so on data
$skey = Array ();
$length = Array ();
$co = count ($arr);
$p = $co/$num; Find out the length of the data to be fetched
for ($j =0; $j < $p; $j + +) {
$offset = ($J-1) * $NUM; Offset, just like paging, an array I take out of the offset is the information for a product.
if ($j ==0) {
$length [] = Array_slice ($arr, 0, $num);
}else{
$length [] = Array_slice ($arr, $num + $offset, $num);//What fields and products are taken out
}
}
$arrtitle = Array ();
$arrfileds = Array ();
$arrtagname = Db::select (' Field id ', ' field name ')->from (' Field table ')->fetch_all ();
foreach ($arrtagname as $value) {
$arrfileds [$value [' fileds_tags ']] = $value [' Fileds_name '];
}
foreach ($fileds as $v)
{
$temarr = Explode ('-', $v);
if (isset ($temarr [0]) &&!empty ($temarr [0])) {
if (Isset ($temarr [1]) &&!empty ($temarr [1])) {
if ($temarr [1] = = ' Wenben ') {
$arrtitle [] = $arrfileds [$temarr [0]]. ' Text ';
}
} else {
if ($temarr [0]!= ' pic ') {//Is to remove the field is the picture to be removed
$arrtitle [] = $arrfileds [$temarr [0]];
}
}
}

}

$skey = Array ();
$order = Array ();
$order [] = ' act_tag ';
$order [] = ' channel_tag ';
$order [] = ' created_time ';
$order [] = ' by ';
$rows = ';
$f = $co/$num//How many items
for ($p =0; $p <count ($arrtitle); $p + +) {
Here is according to their own needs to find their own data, through the user needs of the commodity field identification of the corresponding English identification table.
$skey []= db::select (' field ID ')->from (' Field table ')->where (' Field name ', ' = ', $arrtitle [$p])->fetch_row ();
$rows. = $skey [$p] [' field identification ']. ' | '
}
if ($rows) {$rows = RTrim ($rows, ' | ');}
if (!empty ($rows)) {$exrows = explode (' | ', $rows);} else{$exrows = Array ();}
$skeys = Array_merge ($order, $exrows);
$count 1 = count ($skeys); Number of fields
if (!empty ($length)) {
for ($x =1; $x < $f; $x + +) {//Find out how many pieces of merchandise on the cycle how many times
$orders = Array ();
$orders [] = $act _tag;
$orders [] = $channel _tag;
$orders [] = time ();
$newlen = Array_merge ($orders, $length [$x]);
if ($count 1!== count ($newlen)) {//If the length of the commodity field and the length of the product show that the user has not entered the field
$newrs = Array ();
echo "<script type=\" text/javascript\ >alert (\ <font color= #f00;>.) Please check the first, '. ($x-1). ' Piece of merchandise! '.' Import failed! '." </font> "); </script> ";
Fclose ($handle);
Exit ();
}else{//start
$arrimport = Array_combine ($skeys, $newlen); If two arrays are equal I merge the array and change the date in the import CSV to the timestamp store to the database
if (!empty ($arrimport [' start_time '])) {$sta = Strtotime ($arrimport [' start_time ']);} else{$sta = (int) 0;}
if (!empty ($arrimport [' end_time '])) {$end = Strtotime ($arrimport [' end_time ']);} else{$end = (int) 0;}
$arrtime =array (' start_time ' => $sta, ' end_time ' => $end);
if (!empty ($arrimport [' start_time ']) &&!empty ($arrimport [' end_time '])) {
$newrs =array_merge ($arrimport, $arrtime);
}else{
$newrs = Array ();
echo "<script type=\" text/javascript\ >alert (\ <font color= #f00;>.) Please check the first, '. ($x-1). ' Piece of merchandise! '.' Import failed! '." </font> "); </script> ";
Fclose ($handle);
Exit ();
}
if (count ($skeys) = = count ($newrs)) {
Db::insert (' Commodity table ', Array_values ($skeys))
->values (Array_values ($newrs))
->execute ();
}
}//end
}
}
if ($row -1== (int) 0) {
echo "<script type=\" text/javascript\ >alert (\ <font color= #f00;>.) The product you imported is empty! '." </font> "); </script> ";
}else{
echo "<script type=\" text/javascript\ >alert (\ <font color= #f00;>.) Successfully imported '. ' <font color= #f00;> ". ($row-1). " </font> ". ' Piece of merchandise! '." </font> ");
}
Fclose ($handle);
}
?>

The above is my work need to do the CSV import processing, May and your import way different, but part of the code will always help you!
The following is a simple import:
Copy Code code as follows:

<form enctype= "Multipart/form-data" action= "import.php" method= "POST" >
Import templates
<label for= "File Selection" > File selection: </label><input name= "csv_goods" type= "file"/>
<input type= "Submit" value= "Imports" name= "import"/>
</form>
<?php
if (Isset ($_post[' import ')) {

$file = $_files[' csv_goods '];

$file _type = substr (Strstr ($file [' name '], '. '), 1);

Check file format
if ($file _type!= ' csv ') {
Echo ' file is not in the correct format, please upload it again! ';
Exit
}
$handle = fopen ($file [' Tmp_name '], "R");
$file _encoding = mb_detect_encoding ($handle);

Check file encoding
if ($file _encoding!= ' ASCII ') {
Echo ' File encoding error, please upload it again! ';
Exit
}

$row = 0;
while ($data = Fgetcsv ($handle, 1000, ', ')) {
echo "<font color=red> $row </font>"; You can tell how many lines are in total.
$row + +;
if ($row = = 1)
Continue
$num = count ($data);
This will sequentially output data for each cell in each row
for ($i =0; $i < $num; $i + +) {
echo $data [$i]. " <br> ";
The data is processed here
}
}

Fclose ($handle);
}

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.