.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 ?
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);
This is what you need to do:
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);
Hope it helps. Goodluck!