
PHP’s fread allows to read in chunks of strings. If you got a better option please suggest via comments section below. I have used a Regex to replace newline in the CSV file. Then a third point worth mentioning is use PHP native functions wherever possible. So everything boils down to reading chunks (batches) and multi insert. Reading line by line and iterating through a loop will slowdown the process. The above is key in speeding up the overall process.
#IMPORT .CSV INTO LIGHTWRIGHT CODE#
You may have to do some tweaks here and there in this sample code to fit for your case. The use case is rare and so the solution. If you want to insert a million records in few seconds, using PHP script programmatically, then this will definitely help. This is not for people who are going to see in micro, nano seconds. That’s why I have given in the title as just seconds. So, I didn’t go in that direction for this articles. I am well aware about the micro-benchmarking and its pitfalls.

You should go through that to learn about handling CSV files in PHP. I have written an article about all things CSV in PHP. When you are a “ freelance programmer” (not playing a consultant role), you will be thrust upon with all sorts of crazy requirements. But you never know, when you will come across such a situation, a client requirement can come any time.

In general PHP will not have such use cases. In this article we are going to see how we are going to read a CSV file and INSERT records via PHP script. LOAD DATA INFILE is the best option to import a huge CSV file. There will be a lot of criticism for this article. You should try PHP 7 and experience it yourself. I know, I know! How can you put PHP and Fast in the same line? I would say, you are stuck in the past.
