我试图在数据库中存储有关上传文件的详细信息。
目前,单个上传文件的详细信息将进入阵列。我不知道如何将这些信息写入数据库。
我认为必须使用带阵列的foreach循环来完成,但我不知道该怎么做。
请告知。
我的模型看起来像这样:
class download_model extends CI_Model {
//
// Add file
public function add() {
$new_file = array(
'file_author_id' => $this->profil_model->get_user_id(),
'file_author_name' => $this->profil_model->get_firstname().' '.$this->profil_model->get_lastname(),
'file_name' => $this->input->post('file_name'), <-- IN THIS I MUST RUN LOOP TO SEND MULTIPLE VALUES- I HAVE NO IDEA HOW I CAN DO THIS :(
);
$this->db->insert('download', $new_file);
}
}
这是我的上传文件结果:
Array
(
[pageTitle] => Multiple file upload
[uploads] => Array
(
[0] => Array
(
[file_name] => MY_FILE_NAME.txt
[file_type] => text/plain
[file_path] => DIR
[full_path] => DIR/MY_FILE_NAME.txt
[raw_name] => MY_FILE_NAME
[orig_name] => MY_FILE_NAME.txt
[client_name] => MY_FILE_NAME.txt
[file_ext] => .txt
[file_size] => 0.98
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
[1] => Array
(
[file_name] => MY_FILE_NAME_2.txt
[file_type] => text/plain
[file_path] => DIR
[full_path] => MY_FILE_NAME_2.txt
[raw_name] => MY_FILE_NAME_2
[orig_name] => MY_FILE_NAME_2.txt
[client_name] => MY_FILE_NAME_2.txt
[file_ext] => .txt
[file_size] => 0.98
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
)
)
请帮助我或做任何我能做到的事情:(它可以这样做:(?
答案 0 :(得分:0)
你应该稍微改变你的代码。
你的模特应该是这样的:
public function add($userId, $authorName, $fileName) {
$new_file = array(
'file_author_id' => $userId,
'file_author_name' => $authorName,
'file_name' => $fileName,
);
return $this->db->insert('download', $new_file);
}
在你的控制器中你应该像这样做所需的foreach:
public function upload() {
//GETTING YOUR DATA RIGHT HERE
$userId = $this->profil_model->get_user_id();
$authorName = $this->profil_model->get_firstname().' '.$this->profil_model->get_lastname();
foreach ($uploads as $upload) {
$this->model_name->add($userId, $authorName, $upload['file_name']);
}
}
或者如果您想要在该字段中包含所有上传文件的1行,请执行以下操作:
public function upload() {
//GETTING YOUR DATA RIGHT HERE
$userId = $this->profil_model->get_user_id();
$authorName = $this->profil_model->get_firstname().' '.$this->profil_model->get_lastname();
$fileString = '';
$countUploads = count($uploads);
$i = 1;
foreach ($uploads as $upload) {
$fileString .= $upload['file_name']
if ($i < $countUploads) {
$fileString .= ',';
}
$i++;
}
$this->model_name->add($userId, $authorName, $fileString);
}
答案 1 :(得分:0)
使用insert_batch函数
可以应用一种非常简单的方法<?php
public function add() {
//prepare the array of data
$insert = array_map(function($row){
return array(
'file_author_id' => $this->profil_model->get_user_id(),
'file_author_name' => $this->profil_model->get_firstname().' '.$this->profil_model->get_lastname(),
'file_name' => $row['file_name']
);
},$this->input->post('uploads'));
$this->db->insert_batch('download', $insert);
}