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

时间:2014-07-29 11:30:54

标签: php file csv logging

我使用.csv文件来记录文件是否上传到服务器。日志文件如下所示:

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,其他,1001,201407290945481001msg.mp3,201407290945481001vdc.mp3,137,SYN 09:47:13,其他,1001,201407290946411001msg.mp3,201407290946411001vdc.mp3,136,SYN 09:47:50,其他,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

有时文件是同步的,但有时文件由于互联网问题无法同步,我们可以在' G'日志文件的列。我需要读取这个日志文件,并且必须逐个上传文件* .mp3。为此,我在crontab中运行php脚本,必须读取每行中的文件并上传文件。如果文件上传成功,那么只有" Not-syn"字段需要更改为" Syn"。如果该行有" Syn"已经,它什么都不做,转移到下一行。

我写了以下php脚本:

$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($处理);

这是垃圾编程。 我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

这是你需要做的:

if (($handle = fopen("log.csv", "r")) !== FALSE) {  /* Read csv log file */
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
      /* Compare the value in column 'G' (last valid index in the $data array) to 'Syn'
       * If the value in the 'G' column is 'Syn', skip it and move to the next line
       * Function used is strcasecmp() - comparison is case in-sensitive
       */
      if(strcasecmp(trim($data[count($data)-1]), 'Syn') == 0){
         continue; 
      }
    /* I'm just printing out the values here BUT HERE IS WHERE YOU SHOULD PUT YOUR FILE UPLOAD CODE LOGIC */
        echo $data[0] . '<br/>'; 
        echo $data[1] . '<br/>';
        echo $data[2] . '<br/>';
        echo $data[3] . '<br/>';
        echo $data[4] . '<br/>'; 
        echo $data[5] . '<br/>';
        echo $data[6] . '<br/>';
        echo '<br/>';    

    }
}
fclose($handle);

希望它有所帮助。古德勒克!

答案 1 :(得分:1)

最后,我回答了自己的问题。

$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);