Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9195569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:39:22+00:00 2026-06-17T21:39:22+00:00

I am using this function to upload images, and it is working except in

  • 0

I am using this function to upload images, and it is working except in one part. Where there are more then one image for upload, all images get their name from the first image (overwrite is set to off, so CI is adding number at the end of the name). How can I solve this problem?

function img_upload($folder) {
    $this->path = './public/img/' . $folder;
    $imgs = array();
    $count = 0;
    foreach($_FILES as $key => $value):
        $img_name = is_array($value['name']) ? $value['name'][$count] : $value['name'];
        $img_name = $this->char_replace($img_name, '_');
        $count++;
        $config = array(
            'allowed_types' => 'jpg|jpeg|png|gif',
            'upload_path' => $this->path,
            'file_name' => $img_name
        );
        $this->CI->load->library('image_lib');
        $this->CI->image_lib->clear();
        $this->CI->load->library('upload', $config);
        if($key != 'logo'):
            if (!$this->CI->upload->do_upload($key)) {
            } else {
                $image = $this->CI->upload->data();
                $imgs[] = $image['file_name'];
            }
        endif;
    endforeach;

    if(empty($imgs)):
        return FALSE;
    else:
        return implode(',', $imgs);
    endif;
}

Function char_replace is working without a problem.

function char_replace($text, $rep_simbol = " ")
{
    $char = array('!', '&', '?', '/', '/\/', ':', ';', '#', '<', '>', '=', '^', '@', '~', '`', '[', ']', '{', '}');
    return $name = str_replace($char, $rep_simbol, $text);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:39:24+00:00Added an answer on June 17, 2026 at 9:39 pm

    $this->CI->upload->do_upload($key) expects $_FILES['key'] to only contain one file.

    What you can do is, make a copy of $_FILES, loop through it, and for each file set the values of $_FILES['key'].

    function img_upload($folder) {
        $this->path = './public/img/' . $folder;
        $imgs = array();
    
        // Copy of $_FILES
        $thisFiles = $_FILES;
    
        // Loop through copy of $_FILES
        foreach($theFiles as $key => &$value){
            // Create the $_FILES array for each individual file,
            // so that do_upload can read it correctly
            if(!is_array($value['name'])){
                // If it's not an array, make it one,
                // this will make our future code easier
                foreach($value as $kv => &$val){
                    $val = array($val);
                }
            }
    
            // Loop through each file and upload each one
            foreach($value['name'] as $count=>$img_name){
                $img_name = $this->char_replace($img_name, '_');
    
                foreach($_FILES[$key] as $k => &$v){
                    // CodeIgniter will think this is the $_FILES array
                    $v = $theFiles[$key][$k][$count];
                }
    
                $config = array(
                    'allowed_types' => 'jpg|jpeg|png|gif',
                    'upload_path' => $this->path,
                    'file_name' => $img_name
                );
    
                $this->CI->load->library('image_lib');
                $this->CI->image_lib->clear();
                $this->CI->load->library('upload', $config);
    
                if($key != 'logo'){
                    if (!$this->CI->upload->do_upload($key)) {
                    }
                    else {
                        $image = $this->CI->upload->data();
                        $imgs[] = $image['file_name'];
                    }
                }
            }
        }
    
        return !empty($imgs) ? implode(',', $imgs) : FALSE;
    }
    

    NOTE: This is untested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this function to get the featured images: <a href=# rel=prettyPhoto> <?php
I am using html5Uploader to upload images and everything is working fine. The part
I am working on an image upload script and ran into this problem. Using
i am making an upload with Ajaxupload plugin and i am using this function
I am using this function but it is not working. I'm using StoryBoard and
I am working on one of my project where user will upload the image.
I have a script working to upload images without refreshing the page using jquery.form.js
So I'm using this function I made to read images from the users hard
I'm trying to upload an image using jQuery and PHP. I know there are
My upload/crop function is working almost how I want it. Except Im having to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.