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 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 利用pthon计算薄膜结构的光导纳