![]() Perhaps one of the most powerful, and most dangerous, functions available in PHP is the exec() function. Method #3: Using Linux utilities to calculate the number of lines Parsing larger files with a greater number of columns will end up using memory that doesn’t necessarily need to be used, and will take up processing time doing so. For CSV files with a small number of columns, this won’t matter. While this method is certainly faster than the previous, it does still require reading the entire line into memory just to count it. ![]() ![]() While (fgets($this->fh) != false) $linecount++ $fh = fopen('filename.csv','rb') or die("ERROR OPENING DATA") Using the PHP’s filesystem functions - fopen(), fgets(), and fclose() to loop through the file, reading one line into memory at a time, and incrementing the line count as we do so: There is another method for calculating line count that requires less memory. Method #2: Parsing the CSV file line-by-line using PHP And yet almost every forum out there suggests this solution when people ask how to calculate the number of lines in a CSV file. Reading an entire file into memory just to get a line count? While it may not affect your code when parsing smaller files, larger CSV files will most definitely chew up memory and cause the maximum memory limit (designated in php.ini) to stop the script. $linecount = count(file('filename.csv')) This first method uses the file() function to read the contents of the file into an array, from which the count() method can be used to derive the line count: But first, let’s take a look at the PHP methods… Method #1: Parsing the entire CSV file at once using PHP The good news is that the line count can be calculated very quickly using the PHP exec() function and some simple Linux utilities. Unfortunately, they both require using more memory than is necessary - just to get a line count. There are two ways to do this when using PHP for web development. Knowing the CSV line count is useful for a number of reasons, for example, displaying a progress meter while parsing the large file, or estimating how long the parsing process might take. When parsing large text files, particularly CSV files, web developers often need to know the number of lines contained in the file up front.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |