Yii - 如何上传 csv 保存在数据库中?
我在弄清楚如何读取我上传的 CSV 文件时遇到了一些麻烦,可能是我的控制器代码中缺少某些内容。
public function actionImport() {
$model = new Produtos;
$this->render('import', array('model' => $model) );
if( isset($_FILES['csv_file']) ) {
$handle = fopen($_FILES['csv_file']['tmp_name'], 'r');
if ($handle) {
while( ($line = fgetcsv($handle, 1000, ";")) != FALSE) {
$model->codigo = $line[0];
$model->nome = $line[1];
$model->descricao = $line[2];
$model->stock = $line[3];
$model->data_reposicao = $line[4];
$model->save();
}
}
fclose($handle);
}
}
这只是保存了 CSV 中的最后一行...请帮忙!
任何帮助将不胜感激。
谢谢
i'm with some troubles figuring out how to read the CSV file that I upload, probably i'm missing something in my Controller code.
public function actionImport() {
$model = new Produtos;
$this->render('import', array('model' => $model) );
if( isset($_FILES['csv_file']) ) {
$handle = fopen($_FILES['csv_file']['tmp_name'], 'r');
if ($handle) {
while( ($line = fgetcsv($handle, 1000, ";")) != FALSE) {
$model->codigo = $line[0];
$model->nome = $line[1];
$model->descricao = $line[2];
$model->stock = $line[3];
$model->data_reposicao = $line[4];
$model->save();
}
}
fclose($handle);
}
}
This is only saving me the last line in the CSV... please some help!
Any help will be really appreciated.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我认为您错过了文件名,请尝试 $_FILES['csv_file']['tmp_name']
http://php.net/manual/en/features.file -upload.post-method.php供参考。
此外,Yii 确实提供文件处理,请检查 http://www.yiiframework.com/doc/api /1.1/CUploadedFile 供参考
I think You missed the name of file, try $_FILES['csv_file']['tmp_name']
http://php.net/manual/en/features.file-upload.post-method.php for reference.
Also Yii does provide file handling, check http://www.yiiframework.com/doc/api/1.1/CUploadedFile for reference
不要忘记检查您的数据是否已成功验证。
将此代码插入到您的代码之间:
这样您就可以看到出了什么问题。
Don't forget to check whether your data is successfully validated or not.
Insert this code between yours:
So you can see what's going wrong.
$_FILES 是一个数组,其中包含 ['element_name'] 数组。在您的情况下 $_FILES 是 $_FILES['csv_file']['name']、$_FILES['csv_file']['type']、$_FILES['csv_file']['error']、$_FILES 的数组['csv_file']['size'] 和 $_FILES['csv_file']['tmp_name']。
简而言之; $_FILES['csv_file'] 是一个数组。
$_FILES is an array, which contains ['element_name']-array. In your case $_FILES is an array of $_FILES['csv_file']['name'], $_FILES['csv_file']['type'], $_FILES['csv_file']['error'], $_FILES['csv_file']['size'] and $_FILES['csv_file']['tmp_name'].
So for short; $_FILES['csv_file'] is an array.
您一次又一次地保存相同的模型实例...这就是为什么只保存最后一行...您必须为每一行创建一个新模型,即在 while 循环中添加 $model =新产品;
you are saving the same model instance again and again... that is why only the last line gets saved... you will have to create a new model for every line, i.e. in your while loop, add
$model = new Produtos;
每次需要将行插入表时,您都需要初始化模型对象。代码应该是这样的:
You need to initialise your model object every time when you need to insert your row into table. The code should be like:
我知道这是一篇旧帖子,但我刚刚遇到这个问题,只是想帮助可能遇到相同问题的人(黑白问题与 CSV 或文件上传无关)
问题在于 Yii 如何处理保存,
您需要保存前将模型对象的 isNewRecord 属性设置为 true,将主键设置为 NULL,以便每次保存一条新记录。
每当您在循环中保存行时,您都需要执行上述步骤。
I know this is an old post, but I just came across this and just wanted to help someone who might be having the same issue (b/w issue is not with CSV or file upload)
Issue is with how Yii handles saving
You need to set isNewRecord attribute to true and primary key to NULL of model object before saving to save a new record everytime.
You need to do above step whenever you are saving rows in loop.