doujiangao4229 2014-07-29 11:30
浏览 14
已采纳

.csv文件使用php一次处理单行

I use .csv file to keep log whether the file is uploaded to server or not. Log file is as shown in below :

09:40:51,kapilbastu,1001,201407290940041001msg.mp3,201407290940041001vdc.mp3,137,Not_syn 09:44:30,kapilbastu,1001,201407290943351001msg.mp3,201407290943351001vdc.mp3,136,Not_syn 09:46:25,Other,1001,201407290945481001msg.mp3,201407290945481001vdc.mp3,137,Syn 09:47:13,Other,1001,201407290946411001msg.mp3,201407290946411001vdc.mp3,136,Syn 09:47:50,Other,1001,201407290947191001msg.mp3,201407290947191001vdc.mp3,136,Not_syn 11:46:01,kapilbastu,1001,201407291145101001msg.mp3,201407291145101001vdc.mp3,137,Not_syn 13:58:14,kapilbastu,1001,201407291357121001msg.mp3,201407291357121001vdc.mp3,136,Syn

Sometime file is sync but sometime file can't sync due to Internet problem as we can see in the 'G' column of the log file. I need to read this log file and have to upload file *.mp3 one by one. For that i run php script in crontab that have to read file in each line and upload files. If the file is uploaded successfully then only "Not-syn" field need to change to "Syn". If the line has "Syn" already,it do nothing and move to next line.

I wrote following php script:

$a=6;
if (($handle = fopen("/tmp/newfile.csv", "ar+")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

        if ($data[$a] == "Not_syn") {
            $date = $data[$a - 6];
            $from= $data[$a - 5];
            $phoneNumber = $data[$a - 4];
            $vdc = $data[$a - 3];
            $msg = $data[$a - 2];
            $postTo = $data[$a - 1];

            $flag=upload_function( $date, $from, $phoneNumber, $vdc, $msg, $postTo )                
            if($flag)
            {  
                $detail = array($date, $from, $phoneNumber, $msg, $vdc, $postTo, "Syn"); 
                fputcsv($handle , $detail);
            }
            else
            {
                $detail = array($date, $from, $phoneNumber, $msg, $vdc, $postTo, "Not_syn"); 
                fputcsv($handle , $detail);

            }

                return true;
            } 
            else 
            {
                $detail = array($date, $from, $phoneNumber, $msg, $vdc, $postTo, "Not_syn"); 
                fputcsv($handle , $detail);

                return false;
            }
        }
    }

fclose($handle);

which is rubbish programming. How can i solve this problem ?

  • 写回答

2条回答 默认 最新

  • dousong4777 2014-07-30 08:03
    关注

    Finally, I got answer of my own question.

    $myfile = 'newfile.csv';
    
    $file_read = fopen($myfile, 'r');
    $data = array();
    
    while ($line = fgetcsv($file_read, 1000)) {
        $Col_No = count($line) - 1;
        if (strcasecmp(trim($line[$Col_No]), 'Syn') == 0) {
            $data[] = $line;
            continue;
        }
    
        $date = $line[$Col_No - 6];
        $from = $line[$Col_No - 5];
        $phoneNumber = $line[$Col_No - 4];
        $vdc = $line[$Col_No - 3];
        $msg = $line[$Col_No - 2];
        $postTo = $line[$Col_No - 1];
    
        $flag = upload( $date, $from,$phoneNumber,$vdc,$msg, $postTo); //function return true if file uploaded successfully otherwise return false.
    
        for ($i = 0, $k = count($line); $i < $k; $i++) {
            if ($flag) {
                $line[$Col_No] = 'Syn';
            }
        }
        $data[] = $line;
    }
    
    fclose($file_read);
    
    $file_write = fopen($myfile, 'w');
    foreach ($data as $line) {
        fputcsv($file_write, $line);
    }
    fclose($file_write);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制