我有一些代碼上傳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