I don't know Laravel so I cannot comment specifically on the functionality of Storage
. However, I had a look at the documentation, which states the following:
The get method may be used to retrieve the contents of a given file. The raw string contents of the file will be returned by the method
So, assuming the call you made successfully found and read a file, $contents
is a string - and dereferencing $contents[0]
is just going to return the first character of the string. Not what you want, clearly!
I concur wth @JonStirling in the question comments and try to parse the csv directly with fgetcsv()
. Or, if you want to be more object-based you could use SplFileObject()
which can read and iterate over CSV files, given the appropriate flags.
Here's a copy of example #2 from the PHP docs:
$file = new SplFileObject("animals.csv");
$file->setFlags(SplFileObject::READ_CSV);
foreach ($file as $row) {
list($animal, $class, $legs) = $row;
printf("A %s is a %s with %d legs\n", $animal, $class, $legs);
}
You'll need the orignal filepath to pass to SplFileObject()
too of course - this answer should be useful
Finally, if your CSV has empty lines, you might have to add additional flags to SplFileObject
- namely SplFileObject::SKIP_EMPTY
and SplFileObject::READ_AHEAD
You may also opt to use a pre-existing package; e.g: http://csv.thephpleague.com/
Hope this helps! :)