I have a custom php function for my multiple upload function. I cannot figure out how to make it so when I insert the file name to the database that it matches the row id, of that insert.
How can I make it so. When I create a new banner that each image multiple images that each match the there inserted id.
**
Model Function**
public function addBanner($data) {
$this->db->query("INSERT INTO " . $this->db->dbprefix . "banner
SET name = " . $this->db->escape($data['name']) . ",
status = '" . (int)$data['status'] . "'
");
$banner_id = $this->get_id->get_last_id();
if (isset($data['banner_image'])) {
foreach ($data['banner_image'] as $banner_image) {
// Image File Not Working Need Make It
// So When New Image It Matches The Banner_Image_ID
$this->db->query("INSERT INTO " . $this->db->dbprefix . "banner_image
SET banner_id = '" . (int)$banner_id . "',
link = " . $this->db->escape($banner_image['link']) . ",
image = " . $_FILES['userfile']['name'] . "
");
$banner_image_id = $this->get_id->get_last_id();
$this->db->query("INSERT INTO " . $this->db->dbprefix . "banner_image_description
SET banner_image_id = '" . (int)$banner_image_id . "',
banner_id = '" . (int)$banner_id . "',
title = " . $this->db->escape($banner_image['title']) . "
");
}
}
}
CI Controller Function
public function do_upload_banner() {
$this->lang->load('admin/design/banners', 'english');
$valid_formats = array(
"jpg",
"png",
"gif",
"zip",
"bmp",
'ico'
);
$max_file_size = 1024*$this->settings->get('config_file_max_size');
$path = FCPATH . "upload/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['userfile']['name'] as $field => $name) {
if ($_FILES['userfile']['error'][$field] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['userfile']['error'][$field] == 0) {
if ($_FILES['userfile']['size'][$field] > $max_file_size) {
$this->form_validation->set_message('do_upload_banner', "$name is too large!. Please select another Image");
return false;
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$this->form_validation->set_message('do_upload_banner', "$name is not a valid format!. Please select another Image");
return false;
continue; // Skip invalid file formats
}
else
{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["userfile"]["tmp_name"][$field], $path.$name))
$count++; // Number of successfully uploaded file
}
}
}
}
}