lopes_andre Posted December 17, 2010 Share Posted December 17, 2010 Hi, I'm using the "fgetcsv" function to read a CSV file. I need to count the total number of lines in a CSV file and go to X line. Which the best way of doing it? Best Regards, André. Quote Link to comment https://forums.phpfreaks.com/topic/221981-how-to-get-the-line-number-of-a-csv-file/ Share on other sites More sharing options...
trq Posted December 17, 2010 Share Posted December 17, 2010 There is no real good way of doing it. You'll need to open the file, read the entire thing into an array, count the size of the array, then loop back through 9the array) until you get to the line you want. You'll probably have no use for fgetcsv by then. Quote Link to comment https://forums.phpfreaks.com/topic/221981-how-to-get-the-line-number-of-a-csv-file/#findComment-1148645 Share on other sites More sharing options...
lopes_andre Posted December 17, 2010 Author Share Posted December 17, 2010 Hi, Thanks for the reply. It is a good idea to store in one array 200MB of data or should be better to get the lines of the CSV one by one? The deal is that I have a 200MB CSV file that I need to write the INSERT's INTO to put in the database, but I don't know if it is a good idea to store the data in one array or construct the INSERTS one by one in a loop. Best Regards, Quote Link to comment https://forums.phpfreaks.com/topic/221981-how-to-get-the-line-number-of-a-csv-file/#findComment-1148666 Share on other sites More sharing options...
PFMaBiSmAd Posted December 17, 2010 Share Posted December 17, 2010 Why do you need a count of the number of lines and the ability to goto line x if all you need to do is insert the data into a database table? Quote Link to comment https://forums.phpfreaks.com/topic/221981-how-to-get-the-line-number-of-a-csv-file/#findComment-1148667 Share on other sites More sharing options...
lopes_andre Posted December 17, 2010 Author Share Posted December 17, 2010 Hi, Thanks for the reply. I need to get the line number because, the first line of the CSV contain the Headers. It is not strictly necessary but I'm doing a generic class do deal with CSV's. Quote Link to comment https://forums.phpfreaks.com/topic/221981-how-to-get-the-line-number-of-a-csv-file/#findComment-1148669 Share on other sites More sharing options...
PFMaBiSmAd Posted December 17, 2010 Share Posted December 17, 2010 Detecting the first line and either skipping over it or reading the field names from it has nothing to do with a count of the number of the total lines or the ability to goto line x. I would say you are over complicating this. Quote Link to comment https://forums.phpfreaks.com/topic/221981-how-to-get-the-line-number-of-a-csv-file/#findComment-1148675 Share on other sites More sharing options...
lopes_andre Posted December 17, 2010 Author Share Posted December 17, 2010 I reach the same conclusion. The files are very big to handle with goto line X. I have a method that get me the Headers and other method that do me the Inserts Into on-the-fly. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/221981-how-to-get-the-line-number-of-a-csv-file/#findComment-1148684 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.