0

我如何捕获“nicefile.csv”并在我使用表单中的 POST 提交文件后计算文件中的行数?我尝试了类似$csvlinecount = count(file('$_FILES[csv]'));没有结果的东西。原因是我有代码可以在我导入 csv 值之前和之后计算我的数据库中的行数,但我会整理出重复的条目。所以如果知道之前的条目数是 100 并且之后是 200 会很有趣,那么我知道它导入了 100 条新行。但如果我不知道 csv 文件中的行数,我不知道在导入过程中跳过了多少行。

<?php 

if ($_FILES[csv][size] > 0) {

//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");

//loop through the csv file and insert into database
do {
if ($data[0]) {
    mysql_query("INSERT IGNORE INTO wifi (bssid, channel, privacy, ciper, auth, power, essid, latitude, longitude, first_seen, last_seen) VALUES
        (
            '".addslashes($data[0])."',
            '".addslashes($data[1])."',
            '".addslashes($data[2])."',
            '".addslashes($data[3])."',
            '".addslashes($data[4])."',
            '".addslashes($data[5])."',
            '".addslashes($data[6])."',
            '".addslashes($data[7])."',
            '".addslashes($data[8])."',
            '".addslashes($data[9])."',
            '".addslashes($data[10])."' 
        )
    ");
}
} while ($data = fgetcsv($handle,1000,","));

//redirect
header('Location: index.php/upload?success=1'); die;

}

//Count after import
$after = mysql_query("SELECT * FROM wifi");
$num_rows_after = mysql_num_rows($after);


//echo stats
echo "Number of rows before - ";
echo "$num_rows_before";
<br>
echo "Number of rows after - ";
echo "$num_rows_after";

//generic success notice
if (!empty($_GET[success])) { echo "<br><b>Result: Your file is imported!</b><br>"; } 

//Close connection to databse
mysql_close($connect) ; 

?>
4

6 回答 6

1

You can simply add another variable to count number of lines. For instance, like below (see new $linesCount variable):

if ($_FILES[csv][size] > 0) {

//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
$linesCount = 0;
//loop through the csv file and insert into database
do {

if ($data[0]) {
    $linesCount ++;
    mysql_query("INSERT IGNORE INTO wifi (bssid, channel, privacy, ciper, auth, power, essid, latitude, longitude, first_seen, last_seen) VALUES
        (
            '".addslashes($data[0])."',
            '".addslashes($data[1])."',
            '".addslashes($data[2])."',
            '".addslashes($data[3])."',
            '".addslashes($data[4])."',
            '".addslashes($data[5])."',
            '".addslashes($data[6])."',
            '".addslashes($data[7])."',
            '".addslashes($data[8])."',
            '".addslashes($data[9])."',
            '".addslashes($data[10])."' 
        )
    ");
}
} while ($data = fgetcsv($handle,1000,","));

//redirect
header('Location: index.php/upload?success=1'); die;

}

Also, I would recommend to use mysql_real_escape_string function instead of addslashes. I always had a lot of problems with that addslashes function and it simply escape slashes when mysql_real_escape_string covers more SQL Injection techniques.

But the best would be to forget mysql_* functions (those are deprecated already) and use PDO or mysqli instead

于 2013-06-18T11:16:54.897 回答
1

使用下面的代码,您可以获得行数

$upload_temp=$_FILES["school_upload"]["tmp_name"];      
$fileTemp = $upload_temp;    
$lines = file($fileTemp);    
echo count($lines);
于 2013-11-26T05:58:47.880 回答
0

你的代码:

$csvlinecount = count(file('$_FILES[csv]'));

将字符串设置为函数文件的参数,而不是实际的文件内容。

尝试:

$csvlinecount = count(file($_FILES['csv']['tmp_name']));
于 2013-06-18T11:16:17.797 回答
0

在计算行之前,您必须将其拆分为行。之前它只是一个长字符串。

$count = count(explode("\r\n", file_get_contents($_FILES[csv][tmp_name])));
于 2013-06-18T11:13:07.037 回答
0

无需遍历 CSV 的最佳方法是访问服务器的 Linux 后端。来自约翰·里夫

$linecount = exec('perl -pe \'s/\r\n|\n|\r/\n/g\' ' . escapeshellarg($_FILES[csv][tmp_name]) . ' | wc -l');
于 2013-09-08T03:01:27.520 回答
0
echo $linecount = count(file($filename));
于 2013-12-26T13:55:45.823 回答