dongshi6528 2015-08-03 12:32
浏览 39
已采纳

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

I am importing a .csv file with users once a day.
Now I want to add errorhandling if the file can't be imported.
The thing is that I would like to have all the errors during the proces being collected and mailed to me.

If the script encounters an error it shouldn't stop but continue with the rest of the .csv file.

The only thing I have now is a check if the file excists but I'm currently working on the errorhandling part.

Can anyone telle me how to collect and mail the errors in stead of displaying them?

This is what I have sofar...

<?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 );
        }
    }
?>

Keep in mind that I am currently working on the error handling so there is no handling now.

Thanks

  • 写回答

1条回答 默认 最新

  • doulu2576 2015-08-03 12:38
    关注

    A generic approach:

    //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, "
    "));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭