【问题标题】:how to count number of rows in csv file using php? [duplicate]如何使用php计算csv文件中的行数? [复制]
【发布时间】:2014-03-09 02:32:02
【问题描述】:

我创建了一个 php 页面,在其中我将 csv 文件填充到复选框,我如何计算 csv 文件的行数。

这是我的代码:

$file = $fu['filepath'].$fu['filename'];
        $handle=@fopen($file,"r");
        if($handle) {
            while($row = fgetcsv($handle, 1024)){
echo "<input type='checkbox' name='receptionts[]' checked='checked' value='".$row[0].'|'.$row[1] ."' /> ".$row[0]." <br />";
                }
        } 
        else {
            // File doesn't exist. do something.
        }

【问题讨论】:

  • 如何获取总行数?

标签: php csv


【解决方案1】:

尝试以下方法:

$file = $fu['filepath'].$fu['filename'];
$fileData=@file($file,"r");
$noOfLines = count($fileData);        

if ($noOfLines > 0) {
    while ($row = fgetcsv($handle, 1024)) {
        echo "<input type='checkbox' name='receptionts[]' checked='checked' value='".$row[0].'|'.$row[1] ."' /> ".$row[0]." <br />";
    }
} else {
  // File doesn't exist. do something.
}

希望这会有所帮助。

【讨论】:

  • 它返回 1 但我的 csv 文件包含 5 行
  • 是的,对不起。文件调用是错误的。试试这个 @file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  • 那么如何计算总行数?
  • 请检查我编辑的评论。 :) 我不太习惯这些评论框
  • 非常感谢 DenisR
猜你喜欢
  • 2014-09-07
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 2011-10-14
  • 1970-01-01
  • 2010-10-19
  • 1970-01-01
  • 2011-10-18
相关资源
最近更新 更多