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
I might be missing something but a very simple implementation:
``` $filename = 'example.csv';//or your tmpfilename from $_FILES if importing via form $file = fopen($filename, 'r'); $data = [];
while (($row = fgetcsv($file)) !== false) { $data[] = $row; }
fclose($file);
print_r($data);
``` Its a hop skip and a jump to implement your own field mapping.
I've also recently used:
$csvAsArray = array_map('str_getcsv', file($tmpName));CyberJack77 2 points · Apr 2023
Reading the entire csv into an array can easily lead to memory issues. It is better to use an iterator while reading an unknown amount of data.
tridd3r 1 points · Apr 2023
That's a valid point. I've never really had need to import a csv over a few hundred kb's so never had any issues.
ccrlop [OP] 2 points · Apr 2023
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