我有一些代码上传CSV文件到指定的文件夹,但它不会更新数据库。Codeigniter CSV上传然后爆炸
public function do_upload()
{
$csv_path = realpath(APPPATH . '/../assets/uploads/CSV/');
$config['upload_path'] = $csv_path;
$config['allowed_types'] = '*'; // All types of files allowed
$config['overwrite'] = true; // Overwrites the existing file
$this->upload->initialize($config);
$this->load->library('upload', $config);
if (! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->layout->buffer('content', 'program/upload', $error);
$this->layout->render();
}
else
{
$image_data = $this->upload->data();
$fname = $image_data['file_name'];
$fpath = $image_data['file_path'].$fname;
$fh = fopen($fpath, "r");
$insert_str = 'INSERT INTO wc_program (JobRef, Area, Parish, AbbrWorkType, WorkType, Timing, TrafficManagement, Location, Duration, Start, Finish) VALUES '."\n";
if ($fh) {
// Create each set of values.
while (($csv_row = fgetcsv($fh, 2000, ',')) !== false) {
foreach ($csv_row as &$row) {
$row = strtr($row, array("'" => "\'", '"' => '\"'));
}
$insert_str .= '("'
// Implode the array and fix pesky apostrophes.
.implode('","', $csv_row)
.'"),'."\n";
}
// Remove the trailing comma.
$insert_str = rtrim($insert_str, ",\n");
// Insert all of the values at once.
$this->db->set($insert_str);
echo '<script type="text/javascript">
alert("Document successfully uploaded and saved to the database.");
location = "program/index";
</script>';
}
else {
echo '<script type="text/javascript">
alert("Sorry! Something went wrong please proceed to try again.");
location = "program/upload";
</script>';
}
}
}
当我运行var_dump($ fh);它显示:类型(流)的资源(89)
当我运行var_dump($fpath)
它表明:string(66) "/Applications/MAMP/htdocs/site/assets/uploads/CSV/wc_program.csv"
所以所有上传操作,但什么是错了,没有更新的数据库? 我试过各种改变fopen
方法,但仍然没有喜悦,我真的需要它来添加到数据库和插入查询和设置查询应该做的伎俩,但它没有。
任何帮助非常感谢!
你有'db_debug'在'config/database.php'中设置为'TRUE'吗?它会显示数据库错误。您也可以尝试回显'$ this-> db-> last_query()'来查看它运行的最后一个查询。 – 2013-04-04 20:08:49