Yii 1.1: importcsv

Module to import rows from CSV file to database
38 followers

ImportCSV is used for load positions from CSV file to database.

Import occurs in three steps:

  1. Upload file;
  2. Select delimiter and table;
  3. Select mode and columns in table.

Module has 3 modes:

  1. Insert all - Add all rows;
  2. Insert new - Add new rows. Old rows remain unchanged;
  3. Insert new and replace old - Add new rows. Old rows replace.

All parameters from the previous imports will be saved in a special .php file in upload folder.

Requirements

Yii 1.1

Usage

1) Copy all the 'importcsv' folder under /protected/modules;

2) Register this module in /protected/config/main.php

'modules'=>array(
        .........
        'importcsv'=>array(
            'path'=>'upload/importCsv/', // path to folder for saving csv file and file with import params
        ),
        ......
    ),

3) Create a directory which you use in 'path'. Do not forget to set access permissions for directory 'path';

4) The module is available here:

http://yourproject/importcsv.

Or here:

http://yourproject/index.php?r=importcsv.

Or somewhere else:-) It depends from path settings in your project;

5) ATTENTION! The first row of your csv-file must will be a row with column names.

Our repo in github https://github.com/Ardem/yii-importcsv-extension

Total 20 comments

#18980 report it
Sandip Chandela at 2015/02/17 09:35am
is it working with yii2

I have configured in yii 1.X versions successfully. Is this module working for yii2 ?

#18192 report it
mimo at 2014/09/24 09:59am
With large CSV files

Did anybody face problems importing large CSV files? I keep getting the error - Error: Download file is not a .csv - when i'm selecting a large file.

Any thoughts?

#17729 report it
Lloyd at 2014/07/17 01:03pm
setup

My project separates the code into backend, common, console, and frontend, and I have installed the module in the module folder, a subfolder of the backend folder. When I try and access the module, I get an error that CWebModule can not be found.

I am not sure what I need to do to insure that following php code - class ImportcsvModule extends CWebModule will work.

Please note the the yii framework CwebModule class is located in the folder - \common\lib\Yii\web\CWebModule.

Any help would be greatly appreciated

#15259 report it
belmanski at 2013/10/21 06:08pm
After clicking NEXT button, nothing happens

After installing I can select the cscv file, it uploads correct to the path, but when I click on NEXT button nothing happens....

Any help appreciated.

Thnx

#13995 report it
ardem at 2013/07/11 03:29am
Re: hello frnds...i got this error,please help me ....

Hi Subbu,

You need PHP >= 5.3.0 for using this function. Also you can find more information here: http://php.net/manual/en/function.str-getcsv.php

#13993 report it
subbu vemuri at 2013/07/11 02:25am
hello frnds...i got this error,please help me ....

Call to undefined function str_getcsv() in /var/www/subbu/yiiAddressBook/protected/modules/importcsv/controllers/DefaultController.php on line 192**

#12527 report it
WebDevPT at 2013/03/26 07:44am
I'm unable to use this

I'm able to import via cvs file using phpmyadmin with no problems, but when I use this for the exact file and settings I don't get any warning or error, and nothings gets imported.

Any ideas?

#12449 report it
nirmalroka at 2013/03/21 09:19am
Hi it didn't worked for me

I had raw csv file from excel which has some empty fields, so it is inserting the empty data in new rows

#12419 report it
WebDevPT at 2013/03/19 11:28am
Feature Request

Hi. Please consider adding some error messages when the import is not working. It would help a lot when trying to figure why it didn't work.

#10738 report it
Anthony Gough at 2012/11/20 08:01am
Re: Help With Extension

Thank you heaps for your help. Great module - easy to customise and due to the great commenting very easy to tweak controller and javascript. Best Yii extension I have used.

#10727 report it
ardem at 2012/11/19 10:06am
Re: Help with extension

Good day Anthony,

Yes sure, you could create this import mechanism. You need to go to importcsv/controllers/DefaultController.php. In method "actionIndex" you can remove steps 1 and 2 and create custom condition, which will import to database (part of step 3).

#10715 report it
Anthony Gough at 2012/11/18 10:05pm
Help with extension

I am very new to YII and this has been the easiest extension of all to incorporate - so thanks for a great module. In my specific application the CSV file will always have the exact same layout in terms of delimiters/field mapping - only the length of the CSV will vary (number of rows). Could you guide me in the right direction so I could use this extension however have things such as table name, delimiters, CSV Key etc as constants so the user only has to select the file and does not have to complete the intermediate steps. I see that a importCsvxxx.php file is created containing the parameters from the previous import - I would like to be able to use these parameters for the import so the user does not have to enter these parameters or even see these parameters - Ideally I would like to go from import CSV to insert into database skipping all the various parameter setups. I hope this makes sense.

#10560 report it
Ivanda Nothabeer at 2012/11/04 11:08pm
Re:

My Bad. Importing to a table with an Auto Increment key is working.

#10551 report it
ardem at 2012/11/04 04:58am
Re:

Ivanda, thank you for your review, I'll work on it. Could you please give me a little bit more information about this:

"Your module does not seem to handle cases where a table uses an AutoIncrement primary key that is not included in the CSV file."

#10548 report it
Ivanda Nothabeer at 2012/11/03 10:48pm
Really promising, but too many bugs

Ardem, this is really close to being a great module, but there are some serious issues that need to be resolved ....

  • As pointed out previously, you need to quote ALL field names in your SQL statements. Modifications are required in importCsv.php to complete this. Without this fix SQL statements can sometimes fail.

  • You are not catching errors in the SQL statement execution properly. Whenever an error occurs, Yii's exception handling writes a message in the application log and returns the user to a "Success" message. Hence, there is no user indication that the import has failed.

  • Your module does not seem to handle cases where a table uses an AutoIncrement primary key that is not included in the CSV file. (or at least I could not get it to work)

#10446 report it
ardem at 2012/10/29 09:15am
New release

Thanks for all reviews. I made a new release of this extension. And we have a repository in Github now. You could fork it if you want.

#8667 report it
Qenozis at 2012/06/18 07:11am
Issues confirmed

Unfortunately many issues in this module. It is interesting module, but still many bugs in it - can't make it through second step, it doesn't show anything, just loading some css code as a result.

To fix uploading issue in my case I had to change in download.js

from:

/importcsv/default/upload

to:

index.php?r=importcsv/default/upload

Hope You are still working on it and reading reported issues to solve these bugs.

PS: more flexible way to handle different types of urls would be to pass urls from php to js script as parameter, becouse in php (Yii) You can generate proper url for the project.

Edit: Thx to lxvi I was able to solve my issue in second step by changing in DefaultController->actionIndex from:

$filecontent  = explode("\n", file_get_contents($uploadfile));
$csvFirstLine = explode($delimiter, $filecontent[0]);

to:

$file = fopen($uploadfile, "r");
$csvFirstLine = fgetcsv($file, 0, $delimiter, '"');
fclose($file);
#8614 report it
lxvi at 2012/06/14 08:39am
many many issues with this module!

Hello ardem,

While you have obviously put a lot of work into this module, it has taken me a lot of work to get it function properly in my project. I suspect many others are having these kinds of issues too. I hope you are listening to all of the recommendations people have been making to improve your code and your documentation.

Like edoardo849, I had to remove the leading slash from the action in download.js

I had to put upload/importCsv directly under the project directory instead of under protected/modules/importcsv

I had to change the link in views/default/index.php for the 'Start over' breadcrumb from "/importcsv" to array("/importcsv")

I had a lot of problems because you don't handle the case where cell values are quoted. OpenOffice always encloses cell values in single or double quotes, and simply doing explode($delimiter, $filecontent[$i]) results in broken output from secondResult.php. You need to do str_getcsv($filecontent[$i], $delimiter, $quotecharacter) for users who have csv files with quoted values. You need to use file() instead of file_get_contents() addslashes() explode()

I also had a lot of problems because there were SQL errors being reported in application.log even though your module was saying "Import was carried out". The SQL errors were a result of your failure to quote table and column names with backticks in InsertAll().

You should explicitly say that the first row of the csv file must have column names.

You should explain what you mean by "pole" and how to configure the key comparison (or how to skip key comparison) and column matching.

This stuff might seem really obvious to you since you've been working on it, but anything you can do to help people get up and running quickly will earn you good karma.

#8597 report it
edoardo849 at 2012/06/13 03:30pm
file not imported

Hi, so I solved a little bug in download.js on this line

action : '/importcsv/default/upload',

to

action : 'importcsv/default/upload',

so now it's ok but at the end it says that everything is ok but actuallt nothing is imported... Someone has this problem too?

#8385 report it
marcovtwout at 2012/05/30 09:45am
Code quality

ImportcsvModule.php says

"error_reporting(E_ALL & ~E_NOTICE);"

Not nice..

Leave a comment

Please to leave your comment.

Create extension
Downloads