我尝试将csv
文件上传到Laravel中的表users
。
这是我的php
脚本:
private function _import_csv($path, $filename)
{
$csv = $path . $filename;
$query = sprintf("LOAD DATA local INFILE '%s' INTO TABLE users FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\\n' IGNORE 0 LINES (`firstname`, `lastname`, `username`, `gender`, `email`, `country`, `ethnicity`, `education` )", addslashes($csv));
return DB::connection()->getpdo()->exec($query);
}
public function uploadUsers(){
if (Input::hasFile('fileInput')){
$file = Input::file('fileInput');
$name = time() . '-' . $file->getClientOriginalName();
$path = 'public/uploads/'.date('Y/m/');
$file->move($path, $name);
$this->_import_csv($path, $name);
}
return Response::json(["success"=>true]);
}
但是我收到了这个错误:
{"error":{"type":"ErrorException","message":"PDO::exec(): LOAD DATA LOCAL INFILE forbidden","file":"C:\\wamp\\www\\lc2\\laravel\\app\\controllers\\UsersController.php","line":224}}
我不知道什么是错的。有人可以帮帮我吗?
答案 0 :(得分:20)
请尝试
设置PDO连接选项
在 database.php
中设置属性 PDO :: MYSQL_ATTR_LOCAL_INFILE'mysql' => array(
....
'options' => [PDO::MYSQL_ATTR_LOCAL_INFILE=>true],
),
答案 1 :(得分:-2)
尝试在sprintf函数之前清理查询字符串。