开发者

Validating Uploaded Files Server Side

I have some CSV files that I need uploaded to a site I'm writing in CodeIgniter.

I need to validate the CSV to make sure they contain various 开发者_开发知识库info, column counts match up and stuff like that.

Does CI have any sort of plugin to make this easy?


After the file is uploaded, open it and use fgetcsv to go through it line by line.

http://us3.php.net/manual/en/function.fgetcsv.php

It creates an array (in that link, the array in the first example is called $data), if you are looking for column count, you can find it with sizeof($data). If you need specific column content or types, you can use a wide variety of regex to figure it out. Say column 3 has to be an email address:

$column_size = 8;

while($data=fgetcsv($p))
{
    if ( sizeof($data) < $column_size )
    {
        // handle wrong column count error here
    }

    if ( !is_email($data[2] ) // is_email is a fictional function
    {
        // handle error here
    }

    // other checks...
}

I don't know if there is a CI plugin for it, but it probably couldn't make it much easier anyway.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜