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

comments

lawyeruphitthegym

I'd recommend https://packagist.org/packages/league/csv. You can find docs/examples here https://csv.thephpleague.com/.

RealWorldPHP

This. The League has a lot of great packages, and it would be the first place I would look. If the OP wants an out-of-the-box solution that will fit their exact situation, then it will be very difficult to find anything suitable. But it can be coded to work for any CSV issue using this package.

tridd3r

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

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

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]

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

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]

Thanks for the example and elaboration 👍😊 will be mindful of this!

RealWorldPHP

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

ccrlop [OP]

Thanks all of you …. Truly appreciate the help provided 🙏😊…. Will try these options!

[deleted]

[deleted]

ccrlop [OP]

Thanks 👍