我正在为我的wordpress网站做一个插件。因为我需要在“ID,名称,移动,电子邮件,年龄,性别”格式中导入csv文件中的联系人,因此它将自动进入数据库。我使用的代码是:
<?php global $wpdb;
$mainurl = get_option('siteurl')."/wp-admin/admin.php?page=add_admin_menu_import_contact";
if($_POST['importtrue'] == "true" ) {
if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
$uploadedfile = $_FILES['import_contact']['name'];
$uploaded_file_type = $_FILES['import_contact']['type'];
$allowed_file_types = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel','text/plain');
//$path= EMAIL_PLUGIN_DIR.'/import_contact/';
if(in_array($uploaded_file_type, $allowed_file_types)) {
// Options array for the wp_handle_upload function. 'test_upload' => false
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload($_FILES['import_contact'], $upload_overrides);
$csv_titles = array( 'eemail_patient_id' => array('ID', 'id'),
'eemail_name_sub' => array('name', 'NAME','Name'),
'eemail_mobile_sub' => array('Mobile', 'phone','Phone','MOBILE'),
'eemail_email_sub' => array('email', 'emailid','EMAIL','Email'),
'eemail_age_sub' => array('Age', 'Old','AGE'),
'eemail_gender_sub' => array('Gender', 'gender','GENDER') );
if ( $movefile ) {
echo "<span style='color:green'>File is valid, and was successfully uploaded.</span>\n";
// var_dump( $movefile);
print_r($csv_titles); echo '<br/>';
if (($handle = fopen($movefile["file"], "r")) !== FALSE)
{
$header = fgetcsv($handle, 1000, ",");
$dbkey = array();
foreach($header as $key) {
echo $key;
foreach ($csv_titles as $keys => $vals)
{
echo '<br/>'.$keys;
echo '<br/>';
print_r($vals);
if (in_array ($key,$vals))
{
$dbkey[]=$keys;
}
else
{
continue;
}
}
}
// echo '<br/>Dbkey: '; var_dump($dbkey);
if(! empty($dbkey)) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$i = 0;
$values = array();
foreach($dbkey as $key) {
if (!empty($data[$i])) {
$values[$key] = $data[$i];
$i++;
}
}
$keys = "`" . implode("`, `", array_keys($values)) . "`";
$values = "'" . implode("', '", $values) . "'";
$sql = "INSERT INTO ".WP_eemail_TABLE_SUB." ({$keys}) VALUES ({$values})";
$wpdb->get_results($sql);
}
$usql="update ".WP_eemail_TABLE_SUB." set patient_date= CURDATE() where patient_date='0000-00-00' ";
$wpdb->get_results($usql);
}
else { echo "<span style='color:red'>But provide valid titles for CSV in first row to import contacts </span>";}
fclose($handle);
}
// unlink($movefile["file"]);
}
} else
{
echo "Not an allowed type!\n";
}
}
?>
<script language="JavaScript" src="<?php echo emailnews_plugin_url('inc/setting.js'); ?>"></script>
<form name="form_importcontact" enctype="multipart/form-data" accept-charset="utf-8" method="post" action="admin.php?page=add_admin_menu_import_contact" onsubmit="return importcontact_submit()" >
<table width="100%">
<tr>
<td align="left" valign="middle" width="10%">Select CSV File:</td>
<td align="left" valign="middle"><input name="import_contact" id="import_contact" type="file" /></td>
</tr>
<tr>
<td height="35" colspan="2" align="left" valign="bottom">
<table width="100%">
<tr>
<td width="50%" align="left">
<input name="publish" lang="publish" class="button-primary" value="Import Contacts" type="submit" />
<input name="publish" lang="publish" class="button-primary" onclick="_cancel_import()" value="Cancel" type="button" />
</td>
<td width="50%" align="right"> </td>
</tr> <input type="hidden" id="importtrue" name="importtrue" />
</table></td>
</tr>
</table>
</form>
</div>
我总是得到的输出是:
文件有效,并已成功上传。但在第一行提供有效的CSV标题以导入联系人
当我输出数组时:
$csv_titles: Array ( [eemail_patient_id] => Array ( [0] => ID [1] => id ) [eemail_name_sub] => Array ( [0] => name [1] => NAME [2] => Name ) [eemail_mobile_sub] => Array ( [0] => Mobile [1] => phone [2] => Phone [3] => MOBILE ) [eemail_email_sub] => Array ( [0] => email [1] => emailid [2] => EMAIL [3] => Email ) [eemail_age_sub] => Array ( [0] => Age [1] => Old [2] => AGE ) [eemail_gender_sub] => Array ( [0] => Gender [1] => gender [2] => GENDER ) )
$key: ID Name Mobile Email Age Gender
echo 'keys'.$keys
和print_r($ vals)的输出:
keys: eemail_patient_id
Array ( [0] => ID [1] => id )
keys: eemail_name_sub
Array ( [0] => name [1] => NAME [2] => Name )
keys: eemail_mobile_sub
Array ( [0] => Mobile [1] => phone [2] => Phone [3] => MOBILE )
keys: eemail_email_sub
Array ( [0] => email [1] => emailid [2] => EMAIL [3] => Email )
keys: eemail_age_sub
Array ( [0] => Age [1] => Old [2] => AGE )
keys: eemail_gender_sub
Array ( [0] => Gender [1] => gender [2] => GENDER )
我发现第二个in_array
if条件没有执行,因此#dbkey为空。但我无法找出为什么in_array总是假的原因。从逻辑上讲,这似乎对我来说是正确的。请帮我找原因?
提前谢谢!
答案 0 :(得分:2)
执行“echo $ key;”时你有“ID名称移动电子邮件年龄性别”。它是一个字符串。所以in_array($ key,$ vals)永远不会返回true。因为$ vals与“ID Name Mobile Email Age Gender”字符串不匹配。显示你的实际错误。请爆炸$ key并使用in_array()函数:)