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

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答