關於PHP 讀取CSV入庫的那點事情

想利用一下某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";
?>



發佈了18 篇原創文章 · 獲贊 4 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章