Here I am trying to insert multiple images into the database but they are not inserted. The images are uploading to the folder correctly but don't know why its not entering in to the database.
Here is my image upload function
public function multiple_upload_files($path)
{
$images = array();
if(!empty($_FILES['files']['name'])){
$filesCount = count($_FILES['files']['name']);
for($i = 0; $i < $filesCount; $i++){
$_FILES['file']['name'] = $_FILES['files']['name'][$i];
$_FILES['file']['type'] = $_FILES['files']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['files']['error'][$i];
$_FILES['file']['size'] = $_FILES['files']['size'][$i];
$config['upload_path']= './uploads/'.$path.'/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size']= '2000';
$config['max_width'] = '4000';
$config['max_height'] = '6500';
$config['file_name']='upld-file'.time();
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload("file")) {
$images[] = $this->upload->data();
}
else {
redirect('admin/view-product');
}
}
return $images;
}
Here is my inserting code
$path='products';
if($this->multiple_upload_files($path))
{
$img=implode(',',$images);
$data = array('product_name' => $this->input->post('product_name'),'image'=>$img);
$status = $this->Admin_model->db_insert($table='products',$data);
if($status)
{
$this->session->set_flashdata('message','Product added Successfully');
}
else
{
$this->session->set_flashdata('message','Insertion failed');
}
The problem is the images are uploaded to the uploaded folder but not to the database.
You are returning array of images from method but not receiving it. change you code as below
$path='products';
$images= $this->multiple_upload_files($path);
if($images)
{
$img=implode(',',$images);
$data = array('product_name' => $this->input->post('product_name'),'image'=>$img);
$status = $this->Admin_model->db_insert($table='products',$data);
if($status)
{
$this->session->set_flashdata('message','Product added Successfully');
}
else
{
$this->session->set_flashdata('message','Insertion failed');
}
Related
I have website where I upload several photos at a time. In some moment I noticed that, it does not upload all required photos. Then I renamed photos from 1.jpg to 14.jpg in order to see, what does my code upload. It uploads only odd numbers (2.jpg, 4.jpg, 6.jpg ... 14.jpg). Where can be the problem?
public function addpic() {
$manymanyimages = '';
$config['upload_path'] = './assets/img/cars/';
$config['allowed_types'] = 'jpg|png|jpeg|JPG|PNG|JPEG';
$config['overwrite'] = FALSE;
$config['remove_spaces'] = TRUE;
$config['max_size'] = '25000';
$this->load->library('upload', $config);
$filesCount = count($_FILES['files']['name']);
for ($i = 0; $i < $filesCount; $i++) {
$_FILES['filee']['name'] = $_FILES['files']['name'][$i];
$_FILES['filee']['type'] = $_FILES['files']['type'][$i];
$_FILES['filee']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
$_FILES['filee']['error'] = $_FILES['files']['error'][$i];
$_FILES['filee']['size'] = $_FILES['files']['size'][$i];
if (!$this->upload->do_upload('filee')) {
// $this->output->set_status_header(500);
$this->output->set_output(strip_tags($this->upload->display_errors()));
} else {
$fileData = $this->upload->data();
$manyimages = $fileData['file_name'];
}
}
$last_id = $this->db->select('id')->order_by('id',"desc")->limit(1)->get('cars')->row()->id + 1;
$data = array(
'img_name' => $manyimages,
'iki' => $manymanyimages,
'post_id' => $last_id
);
return $this->db->insert('imgs', $data);
}
remove for loop and add foreach
$i = 0;
foreach($_FILES['files']['name'] as $row){
$_FILES['filee']['name'] = $_FILES['files']['name'][$i];
$_FILES['filee']['type'] = $_FILES['files']['type'][$i];
$_FILES['filee']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
$_FILES['filee']['error'] = $_FILES['files']['error'][$i];
$_FILES['filee']['size'] = $_FILES['files']['size'][$i];
if (!$this->upload->do_upload('filee')) {
// $this->output->set_status_header(500);
$this->output->set_output(strip_tags($this->upload->display_errors()));
} else {
$fileData = $this->upload->data();
$manyimages = $fileData['file_name'];
}
$i++;
}
I want to upload my images array in codeigniter. The names of images are name = standimages[]. This is my controller
$config['upload_path'] = './uploads/individual_stands/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '1024';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$config['overwrite'] = FALSE;
echo $this->upload_images($config, $_FILES, 'standimages');
And my function
function upload_images($config, $files, $name) {
if (!file_exists($config['upload_path'])) {
mkdir($config['upload_path'], 0777, true);
}
$filesCount = count($files[$name]['name']);
for ($i = 0; $i < $filesCount; $i++) {
$files['userFile']['name'] = $files[$name]['name'][$i];
$files['userFile']['type'] = $files[$name]['type'][$i];
$files['userFile']['tmp_name'] = $files[$name]['tmp_name'][$i];
$files['userFile']['error'] = $files[$name]['error'][$i];
$files['userFile']['size'] = $files[$name]['size'][$i];
pre($files);
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload($files['userFile'])) {
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$uploadData[$i]['created'] = date("Y-m-d H:i:s");
$uploadData[$i]['modified'] = date("Y-m-d H:i:s");
}
}
if (!empty($uploadData)) {
//Insert file information into the database
$insert = $this->file->insert($uploadData);
return $statusMsg = $insert ? 'Files uploaded successfully.' : 'Some problem occurred, please try again.';
}
}
Any idea why my images are not being uploaded?
You must send input name to do_upload function:
if ($this->upload->do_upload($name) {
You can check this like as it is working for me.
Set your messages according to that.
if(!empty($_FILES['gallery_image']['name'][0]))
{
$j = 0;
for ($i = 0; $i < count($_FILES['gallery_image']['name']); $i++)
{
$target_path = 'images/gallery_images/';
if(!file_exists($target_path))
{
mkdir($target_path);
}
$validextensions = array("jpeg", "jpg", "png");
$ext = explode('.', basename($_FILES['gallery_image']['name'][$i]));
$file_extension = end($ext);
$target_path = $target_path .$ext[0].'_'.time(). "." . $ext[count($ext) - 1];
$j = $j + 1;
if (in_array(strtolower($file_extension), $validextensions))
{
if (move_uploaded_file($_FILES['gallery_image']['tmp_name'][$i], $target_path))
{
$insert_img_query = "INSERT INTO event_gallery (image, event_id) VALUES ('".$ext[0].'_'.time(). "." . $ext[count($ext) - 1]."','".$edited_id."')";
$result = mysqli_query($con,$insert_img_query);
}
else
{
header('Location:index.php'); //Redirect your page as per your requirement.
exit();
}
}
else
{
header('Location:index.php'); //Redirect your page as per your requirement.
exit();
}
}
}
Try to change your code like this:
It convert array of files in single files at a time
$_FILES['userFile']['name'] = $_FILES['userFiles']['name'][$i];
$_FILES['userFile']['type'] = $_FILES['userFiles']['type'][$i];
$_FILES['userFile']['tmp_name'] = $_FILES['userFiles']['tmp_name'][$i];
$_FILES['userFile']['error'] = $_FILES['userFiles']['error'][$i];
$_FILES['userFile']['size'] = $_FILES['userFiles']['size'][$i];
And pass file name like this :
$this->upload->do_upload('userFile');
Maybe do_upload function can't understand data in $files array for moving file in folder
Hope this helps you
change do_upload function parameter with file input name
if ($this->upload->do_upload($name)) {
I can't seem to figure out how to make this multiple image path upload working. I have been trying to fix it for 2 days but no luck.
Problem: when form is submitted it uploads selected numbers of images to 'upload' folder but only inserts path for one image in db table.
// Form Validation Goes Here
} else {
// Image upload starts here
$number_of_files = count($_FILES['uploadedimages']['tmp_name']);
$files = $_FILES['uploadedimages'];
for($i=0;$i<$number_of_files;$i++) {
if($_FILES['uploadedimages']['error'][$i] != 0) {
$this->form_validation->set_message('fileupload_check', 'At least 1 image needed.');
return FALSE;
}
}
$this->load->library('upload');
$config['upload_path'] = FCPATH . 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['uploadedimage']['name'] = $files['name'][$i];
$_FILES['uploadedimage']['type'] = $files['type'][$i];
$_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i];
$_FILES['uploadedimage']['error'] = $files['error'][$i];
$_FILES['uploadedimage']['size'] = $files['size'][$i];
$this->upload->initialize($config);
if ($this->upload->do_upload('uploadedimage')) {
$data['uploadedimage'] = $this->upload->data();
$image_name = $data['uploadedimage']['file_name'];
$data['uploadedimage'] = $image_name;
} else {
$this->form_validation->set_message('upload_error', $this->upload->display_errors());
return FALSE;
}
}
$this->load->model('admin/model_users');
if($query = $this->model_users->insert_property_details($data)) {
redirect('dashboard/property-successfully-posted');
}
Model is:
$insert_images = array(
'property_images' => $data['uploadedimage'],
'property_ref_id' => $id,
);
$this->db->insert('vbc_property_images', $insert_images);
And field name in view file is 'uploadedimage'.
<input type="file" name="uploadedimages[]" accept="image/*" multiple />
$data['uploadedimage'] should be in foreach( $data['uploadedimage'] as $key==>$val) loop for read multiple data.and use 'property_images' => $val for insert data
$data['uploadedimage'] = $image_name;
replace with
$data['uploadedimage'][] = $image_name;
I was able to achieve it by replacing in controller:
if ($this->upload->do_upload('uploadedimage')) {
$data['uploadedimage'] = $this->upload->data();
$image_name = $data['uploadedimage']['file_name'];
$data['uploadedimage'] = $image_name;
}
by
if ($this->upload->do_upload('uploadedimage', $i)) {
$data['uploadedimage'] = $this->upload->data();
$image_name[$i] = $data['uploadedimage']['file_name'];
$data['images'] = implode(',',$image_name);
}
And in model:
'property_images' => $data['uploadedimage'],
by
'property_images' => $data['images'],
I have 2 functions in a controller.
That connects to the form and does validation.
A callback function for image upload.
This only inserts path to one image in db. If I echo print_r from call back function it show all images but from validation function just one.
// Some Validation
} else {
$this->fileupload_check();
$data['uploadedimage'] = $this->upload->data();
$image_name = $data['uploadedimage']['file_name'];
//echo '<pre>';
//print_r($this->upload->data());
$data['uploadedimage'] = $image_name;
$this->load->model('admin/model_users');
if($query = $this->model_users->insert_property_details($data)) {
redirect('dashboard/property-successfully-posted');
}
And callback image upload:
public function fileupload_check() {
$number_of_files = sizeof($_FILES['uploadedimages']['tmp_name']);
$files = $_FILES['uploadedimages'];
for($i=0;$i<$number_of_files;$i++) {
if($_FILES['uploadedimages']['error'][$i] != 0) {
$this->form_validation->set_message('fileupload_check', 'At least 1 image needs to be uploaded in jpeg, png or gif format only.');
return FALSE;
}
}
$this->load->library('upload');
$config['upload_path'] = FCPATH . 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['uploadedimage']['name'] = $files['name'][$i];
$_FILES['uploadedimage']['type'] = $files['type'][$i];
$_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i];
$_FILES['uploadedimage']['error'] = $files['error'][$i];
$_FILES['uploadedimage']['size'] = $files['size'][$i];
$this->upload->initialize($config);
if ($this->upload->do_upload('uploadedimage')) {
$data['uploadedimage'] = $this->upload->data();
$image_name = $data['uploadedimage']['file_name'];
$data['uploadedimage'] = $image_name;
} else {
$this->form_validation->set_message('fileupload_check', $this->upload->display_errors());
return FALSE;
}
}
return TRUE;
}
I tried to pass the value from 'Callback image upload' function to validation function but it only passes one image not all which are selected.
Try this coding ...
public function fileupload_check() {
$Error_bool = TRUE;
$Error = '';
$number_of_files = sizeof($_FILES['uploadedimages']['tmp_name']);
$files = $_FILES['uploadedimages'];
for($i=0;$i<$number_of_files;$i++) {
if($_FILES['uploadedimages']['error'][$i] != 0) {
$Error_bool = FALSE;
$Error = 'At least 1 image needs to be uploaded in jpeg, png or gif format only.';
}
}
$this->load->library('upload');
$config['upload_path'] = FCPATH . 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['uploadedimage']['name'] = $files['name'][$i];
$_FILES['uploadedimage']['type'] = $files['type'][$i];
$_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i];
$_FILES['uploadedimage']['error'] = $files['error'][$i];
$_FILES['uploadedimage']['size'] = $files['size'][$i];
$this->upload->initialize($config);
if ($this->upload->do_upload('uploadedimage')) {
$data['uploadedimage'] = $this->upload->data();
$image_name = $data['uploadedimage']['file_name'];
$data['uploadedimage'] = $image_name;
} else {
$Error_bool = FALSE;
$Error = $this->upload->display_errors();
}
}
if($Error_bool == FALSE) {
$this->form_validation->set_message('fileupload_check', $Error);
return FALSE;
} else {
return TRUE;
}
}
The easiest way is by creating a session and send the $data in it and access that session in another function .
You can destroy the session after that if you want destroy the session
I had to make few changes to it to make it work,
I included the whole callback function code directly in FORM function, And removed the callback function completely.
This also helped in one other thing to. Earlier in case of form validation error images were being uploaded to upload folder but now they don't.
I had to make little change in do_upload (by adding $i & implode) which helped me insert multiple images path in db row.
The Whole Script Can Be Found At SOLVED
I need some assistance with how to save path for multiple images saved in 'upload' folder in a table. I am totally lost here.
I have this app which uploads multiple pictures perfect, but they are not linked with db anywhere.
This is controller which is actually a callback_ function:
private $_uploaded;
public function fileupload_check() {
$number_of_files = sizeof($_FILES['uploadedimages']['tmp_name']);
$files = $_FILES['uploadedimages'];
for($i=0;$i<$number_of_files;$i++) {
if($_FILES['uploadedimages']['error'][$i] != 0) {
$this->form_validation->set_message('fileupload_check', 'At least 1 image needed.');
return FALSE;
}
}
$this->load->library('upload');
$config['upload_path'] = FCPATH . 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['uploadedimage']['name'] = $files['name'][$i];
$_FILES['uploadedimage']['type'] = $files['type'][$i];
$_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i];
$_FILES['uploadedimage']['error'] = $files['error'][$i];
$_FILES['uploadedimage']['size'] = $files['size'][$i];
$this->upload->initialize($config);
if ($this->upload->do_upload('uploadedimage')) {
$this->_uploaded[$i] = $this->upload->data();
} else {
$this->form_validation->set_message('fileupload_check', $this->upload->display_errors());
return FALSE;
}
}
return TRUE;
}
How do I save the path for each image in db if I have a field in tabel 'images_path'?
Thanks in advance.
In if condition Try this code.
$data['upload_data'] = $this->upload->data();
$image_name = $data['upload_data']['file_name'];
and if you print $data like this:
echo "<pre>"; print_r($data);
you will get whole array which includes path , name , image tyepe etc.