PHP错误处理通过电子邮件导入.csv

时间:2015-08-03 12:32:48

标签: php wordpress csv import

我每天都会向用户导入一个.csv文件 现在我想添加错误处理,如果文件无法导入 问题是我希望收集过程中的所有错误并邮寄给我。

如果脚本遇到错误,它不应该停止,而是继续使用.csv文件的其余部分。

我现在唯一能做的就是检查文件是否有问题,但我目前正在处理错误处理部分。

有人可以告诉我如何收集和邮寄错误而不是显示错误吗?

这就是我所熟悉的......

<?php
    $filepath = get_bloginfo('template_directory')."/import.csv";
    setlocale(LC_ALL, 'nl_NL');
    ini_set('auto_detect_line_endings',TRUE);
    $file = fopen($filepath, "r") or die("Error opening file");
    $i = 0;

    while(($line = fgetcsv($file, 1000, ";")) !== FALSE) {
        if($i == 0) {
            $c = 0;
            foreach($line as $col) {
                $cols[$c] = $col;
                $c++;
            }
        } else if($i > 0) {
            $c = 0;
            foreach($line as $col) {
                $data[$i][$cols[$c]] = $col;
                $c++;
            }
        }
        $i++;
    }
    echo '<pre>';
    print_r($data);
    echo '</pre>';
    foreach ($data as $gebruiker){
        $username = $gebruiker['username'];
        if ( username_exists( $username ) && strtolower($gebruiker['status']) == 'published' ){
            // IF USER EXISTS: UPDATE
            $user = get_user_by( 'login', $username);
            update_user_meta( $user->ID, 'first_name', $gebruiker['first_name'] );
            update_user_meta( $user->ID, 'surname_prefix', $gebruiker['surname_prefix'] );
            update_user_meta( $user->ID, 'last_name', $gebruiker['last_name'] );
            update_user_meta( $user->ID, 'company', $gebruiker['bedrijf'] );
            update_user_meta( $user->ID, 'function', $gebruiker['functie'] );
            update_user_meta( $user->ID, 'region', $gebruiker['regio'] );
            update_user_meta( $user->ID, 'market', $gebruiker['product_doelgroep'] );
            update_user_meta( $user->ID, 'phone', $gebruiker['telefoonnummer'] );
            update_user_meta( $user->ID, 'provincie', $gebruiker['provincie'] );
            $wpdb->update($wpdb->users, array(  'user_email'    =>  $gebruiker['email'], 'user_registered'  =>  date("Y-m-d H:i:s")), array('ID' => $user->ID));
        }else{
            $empty_surname_prefix = ($gebruiker['surname_prefix'] == ' ' ? '' : $gebruiker['surname_prefix'].' ');
            $users = wp_insert_user(
                array( // ADD NEW USER TO DATABASE
                    'user_login'    =>  $gebruiker['username'],
                    'user_pass'     =>  $gebruiker['password'],
                    'first_name'    =>  $gebruiker['first_name'],
                    'last_name'     =>  $empty_surname_prefix . $gebruiker['last_name'],
                    'user_email'    =>  $gebruiker['email'],
                    'display_name'  =>  $gebruiker['first_name'] . ' ' . $gebruiker['last_name'],
                    'nickname'      =>  $gebruiker['first_name'] . '' . $gebruiker['last_name'],
                    'role'          =>  'subscriber'
                )                       
            );
            foreach ($data as $update_user) {
                    // ADD ADDITIONAL DATA TO JUST CREATED USER
                    update_user_meta( $users, 'company', $gebruiker['bedrijf'] );
                    update_user_meta( $users, 'function', $gebruiker['functie'] );
                    update_user_meta( $users, 'region', $gebruiker['regio'] );
                    update_user_meta( $users, 'market', $gebruiker['product_doelgroep'] );
                    update_user_meta( $users, 'phone', $gebruiker['telefoonnummer'] );
                    update_user_meta( $users, 'provincie', $gebruiker['provincie'] );
            }
        }if(username_exists( $username ) && strtolower($gebruiker['status']) == 'archived'){// DELETE USER IF STATUS IS ARCHIVED
            require_once(ABSPATH.'wp-admin/includes/user.php' );
            $user = get_user_by( 'login', $username);
            echo $user->ID.'<br>';
            wp_delete_user( $user->ID );
        }
    }
?>

请记住,我目前正在处理错误处理,因此现在没有处理。

由于

1 个答案:

答案 0 :(得分:1)

通用方法:

//create an array to hold errors:

$errors = [];

//when en error occurs, add it to the array
if($somethingWentWrong){
    $errors[] = 'The error message goes here';
}

//when finished, check for errors and email if found

if(count($errors) > 0){
    mail('hello@somedomain.com', 'some errors happened', implode($erros, "\n"));
}