当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Count lines in csv-file after submitting in form

问题描述:

How could i capture the "nicefile.csv" and count the rows in the file after i submit it with POST from a form? I tried something like $csvlinecount = count(file('$_FILES[csv]')); without result. The reason is i have code that counts amout of rows in my database before and after i import the csv-values, but im sorting out duplicate entries. So it would be fun to know if the before number of entries is 100 and after it´s 200, then i know it imported 100 new lines. But if i dont know the amount of lines in the csv-file, i dont know how many it skipped during the import.

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

?>

网友答案:

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

网友答案:

using the below code you can get number of lines

$upload_temp=$_FILES["school_upload"]["tmp_name"];      
$fileTemp = $upload_temp;    
$lines = file($fileTemp);    
echo count($lines);
网友答案:

Before you can count the rows you have to split it into rows. Before it is just a long string.

$count = count(explode("\r\n", file_get_contents($_FILES[csv][tmp_name])));
网友答案:

Your code:

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

sets as param for function file a string not the actual file content.

Try:

$csvlinecount = count(file($_FILES['csv']['tmp_name']));
网友答案:

The best way to do this without having to iterate through the CSV is to the Linux backend of your server. From John Reeve:

$linecount = exec('perl -pe \'s/\r\n|\n|\r/\n/g\' ' . escapeshellarg($_FILES[csv][tmp_name]) . ' | wc -l');
网友答案:
echo $linecount = count(file($filename));
分享给朋友:
您可能感兴趣的文章:
随机阅读: