CSV importer with field mapping
Anyone know of a good csv importer script or class i can use to integrate into my web app. Am looking to allow my users to import data from a csv file to mysql database.
Also if possible to allow them field mapping. Appreciate if someone can guide me to a script, class or paid script. Couldnt find anything suitable on codecanyon.
Thanks in advance 🙏
11 comments · 83% upvoted · other discussions
Most of my users would import not more than 30 rows. Maybe can put a restriction in the code to avoid mem issues i guess. Thanks 🙏
CyberJack77 2 points · Apr 2023
That will be hard to do because you still need to read the entire file. If the file is too big, you will run out of available memory, without being able to detect it upfront. You can check the file size before importing, but that isn't always enough.
I always use something like this to import CSV files: https://pastebin.com/PjBcBzc4
The CsvReader class uses a SplFileObject as input and implements an Iterator which can be used to loop the data. The first line of the CSV will be used for the keys, and the class has various options like setting the delimiter. The SplFileObject reads a CSV using an iterator, so this way you won't get memory issues, even when using big files.
The pastebin contains an example using a temporary CSV file, but it can easily be converted to use a real CSV file.
ccrlop [OP] 1 points · Apr 2023
Thanks for the example and elaboration 👍😊 will be mindful of this!
RealWorldPHP 2 points · Apr 2023
Or, instead of checking for the 30 row limit, just check the file size. You don't have to read in the whole file into memory to do that, and it would let you know if it was too large to deal with.
https://www.php.net/manual/en/function.filesize.php