Newbie Question: What's the best way to process through uploaded flat file?

I have been working with yii for a couple weeks and I am finally getting the hang of it, at least whenever I can find an example of something similar to what I am trying to do somewhere.

But I still have some problems with some basics.

I need to be able to allow the user to upload a csv file or a flat file from his machine. Then I need to read the these files record by record to validate and parse them and store selected values in keyed files in my database.

I have the user upload form working. I have the keyed file data destination models (and CRUD screens) working. I have the methods that validate and parse the files written, but not tested yet.

What I’m stuck on is how to best use yii to save and then read through and process the uploaded files. I’m used to simply saving the file, opening the file, reading each record into a string, then processing each string in a loop.

I’m not sure what the best way to go about this is with yii. The files could get very big, so storing them in memory won’t work. There is no reason to define these uploaded flat/csv files in a database, so I don’t think ActiveRecord is appropriate.

I know I’m once again missing something very simple. Could someone please point me in the right direction?

Can you define a model for each row in that CSV? I mean: Do they all share the same attributes and rules? If so, you could extend from CModel, define some rules and maybe add a method loadAttributesFromCsv($csvRow) that populates that model. You could then use validate()/getErrors() on each row, like you would with CFormModel or AR. Not sure if that’s your goal, though.

Thanks for the reply.

The advice I’m looking for is more basic than this. I want to know how to use yii to simply read a flat file that is not defined in a database record by record.

My database holds file format definitions for variable flat file formats. The user picks the appropriate definition record when uploading the flat file, then I need to process through the flat file using the database stored file format definitions to load the flat file data into a keyed database table.

My problem is that I don’t know how to accomplish something very basic which is how to loop through an uploaded flat file that is not stored or defined in my database.

Have you checked out this extension: CFile

I’ve found it to be quite easy to utilize. It won’t do the line by line that you need, but you should be able to figure out how to extend in the functionality that you need. =)

You can also use straight PHP:




$lines=file('myfile.csv');


if ($lines===false)

  die('error opening file!');


foreach($lines as $line) {

  echo $line; // do something

}