想利用一下某OA的读取CSV文件入库的问题,结果发现反编译的源码问题多多啊,呵呵,自己修改了一些,留存备忘。
function CSV2Array( $content, $title = array( ), $delimiter = ",", $enclosure = "\"", $optional = 1 )
{
$content = trim( $content );
$content = str_replace( "\r", "", $content );
$csv_array = array( );
$expr_line = "/\\n(?=(?:[^".$enclosure."]*".$enclosure."[^".$enclosure."]*".$enclosure.")*(?![^".$enclosure."]*".$enclosure."))/";
$expr_field = "/".$delimiter."(?=(?:[^".$enclosure."]*".$enclosure."[^".$enclosure."]*".$enclosure.")*(?![^".$enclosure."]*".$enclosure."))/";
$lines = preg_split( $expr_line, trim( $content ) );
foreach ( $lines as $line )
{
$fields = preg_split( $expr_field, trim( $line ) );
$csv_array[] = preg_replace( array( "/\"(.*)\"\$/s", "/\"\"/s", "/\\<\\?/s" ), array( "\$1", "\"", "<?_(" ), $fields );
}
if ( !is_array( $title ) && count( $title ) == 0 || count( $csv_array ) == 0 )
{
return $csv_array;
}
$field_map = array( );
while ( list( $key, $value ) = each( &$title ) )
{
if ( ( $index = array_search( $key, $csv_array[0] ) ) !== FALSE )
{
$field_map[$value] = $index;
}
}
$lines = array( );
$i = 1;
for ( ; $i < count( $csv_array ); ++$i )
{
$line = array( );
reset( &$field_map );
while ( list( $key, $value ) = each( &$field_map ) )
{
$line[$key] = $csv_array[$i][$value];
}
$lines[] = $line;
}
return $lines;
}
utility_all.php 中的CSV2ARRAY;
<?php
//借用一下通讯簿导入,需要修改一下问题就在title=array(),必须要做表格字段的赋值
include_once( "inc/auth.php" );
include_once( "inc/utility_all.php" );
include_once( "inc/utility_field.php" );
echo "<html>\r\n<head>\r\n<title>";
echo _( "分组管理" );
echo "</title>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">\r\n</head>\r\n<body class=\"bodycolor\" topmargin=\"5\">\r\n";
if ( $FILE_NAME == "" )
{
echo "<script Language=\"JavaScript\">\r\nfunction CheckForm()\r\n{\r\n if(document.form1.CSV_FILE.value==\"\")\r\n { alert(\"";
echo _( "请选择要导入的文件!" );
echo "\");\r\n return (false);\r\n }\r\n\r\n if (document.form1.CSV_FILE.value!=\"\")\r\n {\r\n var file_temp=document.form1.CSV_FILE.value,file_name;\r\n var Pos;\r\n Pos=file_temp.lastIndexOf(\"\\\\\");\r\n file_name=file_temp.substring(Pos+1,file_temp.length);\r\n document.form1.FILE_NAME.value=file_name;\r\n }\r\n\r\n return (true);\r\n}\r\n</script>\r\n\r\n <table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"3\" class=\"small\">\r\n <tr>\r\n <td class=\"Big\"><img src=\"/images/sys_config.gif\" align=\"absmiddle\"><span class=\"big3\"> ";
echo _( "导入" );
echo "CSV";
echo _( "量化基础数据" );
echo "</span><br>\r\n </td>\r\n </tr>\r\n </table>\r\n\r\n <br>\r\n <br>\r\n\r\n <div align=\"center\" class=\"Big1\">\r\n <b>";
echo _( "请指定用于导入的" );
echo "CSV";
echo _( "文件:" );
echo "</b>\r\n <form name=\"form1\" method=\"post\" action=\"import.php\" enctype=\"multipart/form-data\" οnsubmit=\"return CheckForm();\">\r\n <input type=\"file\" name=\"CSV_FILE\" class=\"BigInput\" size=\"30\">\r\n <input type=\"hidden\" name=\"FILE_NAME\">\r\n <input type=\"hidden\" name=\"GROUP_ID\" value=\"";
echo $GROUP_ID;
echo "\">\r\n <input type=\"submit\" value=\"";
echo _( "导入" );
echo "\" class=\"BigButton\">\r\n </form>\r\n <br>\r\n <input type=\"button\" value=\"";
echo _( "返回" );
echo "\" class=\"BigButton\" οnclick=\"location='index.php'\">\r\n </div>\r\n";
exit( );
}
if ( strtolower( substr( $FILE_NAME, -3 ) ) != "csv" )
{
message( _( "错误" ), _( "只能导入CSV文件!" ) );
button_back( );
exit( );
}
//字段名称
$title = array( "名称"=>"MC" ,"识别号"=> "SBH", "受理日期"=>"SLRQ", "经办人"=>"JBR" , "主管"=>"ZG" , "附注"=>"NOTES" );
$ROW_COUNT = 0;
$data = file_get_contents( $CSV_FILE );
if ( !$data )
{
message( _( "错误" ), _( "打开文件错误!" ) );
button_back( );
exit( );
}
$lines = csv2array( $data, $title);
//print_r($lines); 如过不做title的字段赋值操作,那么这里只会显示第一个字段名,而且写入数据库时也会只写入一个字段信息。
//print_r($data);
$lines_field = get_field_lines( $data, "LIANGHUA" );
while ( list( $I, $DATA ) = each( $lines ) )
{
++$ROW_COUNT;
$ID_STR = "";
$VALUE_STR = "";
$STR_UPDATE = "";
reset( $title );
foreach ( $title as $key )
{
//这里修改掉那些不用的if条件语句
$ID_STR .= $key.",";
$VALUE_STR .= "'".$DATA[$key]."',";
$STR_UPDATE .= "{$key}='{$DATA[$key]}',";
}
if ( substr( $STR_UPDATE, -1 ) == "," )
{
$STR_UPDATE = substr( $STR_UPDATE, 0, -1 );
}
$ID_STR = trim( $ID_STR, "," );
$VALUE_STR = trim( $VALUE_STR, "," );
$query = "select LH_ID from LIANGHUA where MC='".$DATA['MC'].( "' and GROUP_ID='".$GROUP_ID."'" );
$cursor = exequery( $connection, $query );
if ( $ROW = mysql_fetch_array( $cursor ) )
{
$LH_ID = $ROW['LH_ID'];
$query1 = "update LIANGHUA SET ".$STR_UPDATE." where LH_ID='".$LH_ID."'";
$cursor1 = exequery( $connection, $query1 );
}
else
{
$query = "insert into LIANGHUA (USER_ID,GROUP_ID,LH_NO,".$ID_STR.( ") values ('{$USER_ID}','".$GROUP_ID."','{$ROW_COUNT}'," ).$VALUE_STR.");";
exequery( $connection, $query );
$LH_ID = mysql_insert_id( );
}
if ( 0 < count( $lines_field[$I] ) )
{
save_field_data( "LIANGHUA", $LH_ID, $lines_field[$I] );
}
}
if ( file_exists( $CSV_FILE ) )
{
@unlink( $CSV_FILE );
}
message( "", _( "共" ).$ROW_COUNT._( "条数据导入!" ) );
echo "<div align=\"center\">\r\n<input type=\"button\" value=\"";
echo _( "返回" );
echo "\" class=\"BigButton\" onClick=\"location='index.php';\" title=\"";
echo _( "返回" );
echo "\">\r\n</div>\r\n\r\n</body>\r\n</html>\r\n\r\n";
?>