Re: CSV file

From: Date: Wed, 24 Jun 2009 16:05:34 +0000
Subject: Re: CSV file
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-294533@lists.php.net to get a copy of this message
Stuart wrote: > 2009/6/24 Jonathan Tapicer <tapicer@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

« previous php.general (#294533) next »