Stuart wrote:
> 2009/6/24 Jonathan Tapicer <tapi...@gmail.com>:
>> If you want to know how many lines there are *before* inserting to the
>> database, you can't count "as you go", you have to either read the
>> file twice or read it once, store it memory in a variable and then
>> insert in the database.
> 
> Do it in bytes rather than lines, then you don't waste time loading
> the file twice and you'll get the same end result.
> 
> -Stuart

  ^Winner!


$file_name = '/path/to/file.csv';
$file_size = filesize($file_name);
$bytes_read = 0;
$new_percentage = 0;

$handle = fopen($file_name, 'r');

if ($handle) {
    while (!feof($handle)) {
        $line = fgets($handle);
        $bytes_read = $bytes_read + strlen($line);

        //insert line into DB
                
        $new_percentage = round($bytes_read / $file_size * 100, 0);

        if ($new_percentage > $percentage) {
                $percentage = $new_percentage;
                echo $percentage . "%\n";
                flush();
        }
    }
    fclose($handle);
}

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to