kenjaW
asked on
How to split a csv file in PHP?
I've got a large csv file that I'm creating using php. At the end of the script, I have a single variable that contains the entire csv file contents (less the header which I have in a separate variable). I want to run the csv file variable through a splitter that will break down the file into chunks of N lines. I upload the files and am file size limited. Obviously, I'd need to split the file after a /r/n so it breaks at the end of a line.
I'd ultimately like a function that looks something like this:
$csv_array=split_csv($num_ lines, $input_csv_file, $csv_header);
but if someone can get me on the right track, I'm happy to code it up and share it.
I'd ultimately like a function that looks something like this:
$csv_array=split_csv($num_
but if someone can get me on the right track, I'm happy to code it up and share it.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Cause one line could be longer than another.
use file_get_contents() to get the file into a string. and the use strlen() to get char count.
after that you write file back into several files with max char count of n.
This way you can control exact file size to upload.