I have a CSV file sent by a stock company daily. I have a update function that takes that data from CSV and update a database. CSV has 50 columns, and about 10,000 Rows. And at the end of the CSV there are few INSTRUCTIONS.
Obviously, those instructions are not to be uploaded to the database, and thus need to be deleted once the file is uploaded.
So the instructions start with a heading INSTRUCTIONS
and then there are about 20-40 rows of instructions.
I want to have a function that will delete anything starting from INSTRUCTIONS will the end of file. Please guide me how to achieve this.
How does your function handle the CSV file?
Does it insert the data in the DB one row at a time? If so, you can check the heading of each line, and not insert the ones starting with INSTRUCTIONS.
if your file have 50 columns and 10,000 rows than you must have same structure in your database also so in any case if you dont have any column for instruction or whatever the unwanted data wont get stored in the database. thats what i think so.
A basic idea, if you want to edit the actual CSV file, would be to look for the INSTRUCTIONS line and truncate the file to that point. For example:
$fp = fopen($file, 'r+');
// Locate the point in the file which is
// a line containing only "INSTRUCTIONS"
$cursor = FALSE;
while ( ! feof($fp))
{
$line = fgets($fp);
if (rtrim($line) == 'INSTRUCTIONS')
{
$cursor = ftell($fp) - strlen($line);
break;
}
}
// Truncate the file if the "INSTRUCTIONS" line was found
if ($cursor !== FALSE)
{
ftruncate($fp, $cursor);
}
fclose($fp);