Reputation: 3875
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?
Upvotes: 0
Views: 661
Reputation: 1539
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
Upvotes: 1
Reputation: 265
change do_upload function parameter with file input name
if ($this->upload->do_upload($name)) {
Upvotes: 0
Reputation: 1578
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();
}
}
}
Upvotes: 1
Reputation: 3354
You must send input name to do_upload
function:
if ($this->upload->do_upload($name) {
Upvotes: 1