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 8521107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:43:24+00:00 2026-06-11T06:43:24+00:00

I have a function in my site that uploads images from the $_FILES array.

  • 0

I have a function in my site that uploads images from the $_FILES array. This function works fine for if you only have a single image, and two or less items in the array, but it can only upload 2 images. For example, my form allows for a maximum of 17 images in the pei_name[] fields but it only uploads the first 2 fields. Here is the function:

private function productImageUploader($id, $image, $altText, $uploadDir)
{

    $uploadDir = (!substr($uploadDir, "/")) ? $uploadDir."/" : $uploadDir;
    $image = $_FILES[$image];

    if(is_array($image['name']))
    {
    print_r($_FILES);
        for($i=0; $i<=sizeof($image['name']); ++$i)
        {
            echo $i;
            if($image['error'][$i] != 4)
            {

                $image['name'][$i] = str_replace(" ", "-", $image['name'][$i]);

                $targetPath = $uploadDir.basename($image['name'][$i]);

                if(!move_uploaded_file($image['tmp_name'][$i], $targetPath))
                {

                    switch($image['error'][$i])
                    {

                        case 1:
                            return "<p class=\"error\">The file for Extra Image {$i} is too large for the server.</p>";
                            break;

                        case 2:
                            return "<p class=\"error\">The file for Extra Image {$i} is too large for the form.</p>";
                            break;

                        case 3:                             
                        case 6:
                        case 7:
                            return "<p class=\"error\">An error occured uploading Extra Image {$i}. Please <a onclick=\"location.reload(true);\">refresh</a> the page.</p>";
                            break;  

                        case 8:
                            return "<p class=\"error\">The file for Extra Image {$i} is not the correct file type. Please convert it to either a jpg, .png or .bmp file.</p>";
                            break;

                    }

                }
                else
                {

                    $thisAlt = (isset($_POST[$altText][$i]) && $_POST[$altText][$i] != "") ? $_POST[$altText][$i] : $_POST['prod_title'];

                    $data = array (
                        'pei_product_id' => $id,
                        'pei_location' => $image['name'][$i],
                        'pei_alt' => $thisAlt
                    );

                    if(!isset($_POST["pei_id"][$i]))
                    {

                        $this->db->insert("product_extra_images", $data);

                    }
                    else
                    {

                        $this->db->where("pei_id", $_POST['pei_id'][$i]);
                        $this->db->update("product_extra_images", $data);

                    }

                    return true;

                }

            }

        }

    }
    else
    {

        if($image['error'] != 4)
        {

            $image['name'] = str_replace(" ", "-", $image['name']);

            $targetPath = $uploadDir.basename($image['name']);

            if(!move_uploaded_file($image['tmp_name'], $targetPath))
            {

                switch($image['error'])
                {

                    case 1:
                        return "<p class=\"error\">The file for the main product image is too large for the server.</p>";
                        break;

                    case 2:
                        return "<p class=\"error\">The file for the main product image is too large for the form.</p>";
                        break;

                    case 3:                             
                    case 6:
                    case 7:
                        return "<p class=\"error\">An error occured uploading the main product image. Please <a onclick=\"location.reload(true);\">refresh the page.</p>";
                        break;  

                    case 8:
                        return "<p class=\"error\">The file for the main product image is not the correct file type. Please convert it to either a jpg, .png or .bmp file.</p>";
                        break;

                }

            }
            else
            {

                $data = array ('prod_main_image' => $image['name']);

                $this->db->where("prod_id", $id);
                $this->db->update("products", $data);

                return true;

            }

        }

    }

    if(isset($_POST["pei_id"]) && is_array($_POST["pei_id"]))
    {

        for($i=0; $i<sizeof($_POST["pei_id"]); ++$i)
        {

            $thisAlt = (isset($_POST[$altText][$i]) && $_POST[$altText][$i] != "") ? $_POST[$altText][$i] : $_POST['prod_title'];
            $this->db->where("pei_id", $_POST["pei_id"][$i]);
            $this->db->update("product_extra_images", array("pei_alt"=> $thisAlt));
        }

    }

    return true;

}

Here is the contents of the $_FILES array:

Array
(
[prod_main_image] => Array
    (
        [name] => 
        [type] => 
        [tmp_name] => 
        [error] => 4
        [size] => 0
    )

[pei_image] => Array
    (
        [name] => Array
            (
                [0] => Untitled.jpg
                [1] => Untitled.jpg
                [2] => Untitled.jpg
                [3] => Untitled.jpg
                [4] => Untitled.jpg
                [5] => Untitled.jpg
                [6] => Untitled.jpg
                [7] => Untitled.jpg
                [8] => Untitled.jpg
                [9] => Untitled.jpg
                [10] => Untitled.jpg
                [11] => Untitled.jpg
                [12] => Untitled.jpg
                [13] => Untitled.jpg
                [14] => Untitled.jpg
                [15] => Untitled.jpg
                [16] => Untitled.jpg
            )

        [type] => Array
            (
                [0] => image/jpeg
                [1] => image/jpeg
                [2] => image/jpeg
                [3] => image/jpeg
                [4] => image/jpeg
                [5] => image/jpeg
                [6] => image/jpeg
                [7] => image/jpeg
                [8] => image/jpeg
                [9] => image/jpeg
                [10] => image/jpeg
                [11] => image/jpeg
                [12] => image/jpeg
                [13] => image/jpeg
                [14] => image/jpeg
                [15] => image/jpeg
                [16] => image/jpeg
            )

        [tmp_name] => Array
            (
                [0] => /tmp/phpGgDKqk
                [1] => /tmp/phpruuIs2
                [2] => /tmp/phpIEbSuK
                [3] => /tmp/phpHDhaxs
                [4] => /tmp/phpigwAza
                [5] => /tmp/phpnEY8BS
                [6] => /tmp/phpEytPEA
                [7] => /tmp/phpRCcEHi
                [8] => /tmp/phpeyUAK0
                [9] => /tmp/php5aQFNI
                [10] => /tmp/php07zSQq
                [11] => /tmp/phppQHdU8
                [12] => /tmp/php8GFGXQ
                [13] => /tmp/phpbaQh1y
                [14] => /tmp/phpqN7Z4g
                [15] => /tmp/phpjPlP8Y
                [16] => /tmp/phpYL3KcH
            )

        [error] => Array
            (
                [0] => 0
                [1] => 0
                [2] => 0
                [3] => 0
                [4] => 0
                [5] => 0
                [6] => 0
                [7] => 0
                [8] => 0
                [9] => 0
                [10] => 0
                [11] => 0
                [12] => 0
                [13] => 0
                [14] => 0
                [15] => 0
                [16] => 0
            )

        [size] => Array
            (
                [0] => 172159
                [1] => 172159
                [2] => 172159
                [3] => 172159
                [4] => 172159
                [5] => 172159
                [6] => 172159
                [7] => 172159
                [8] => 172159
                [9] => 172159
                [10] => 172159
                [11] => 172159
                [12] => 172159
                [13] => 172159
                [14] => 172159
                [15] => 172159
                [16] => 172159
            )

    )

)

  • 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-11T06:43:26+00:00Added an answer on June 11, 2026 at 6:43 am

    The first return true was in the wrong place. It needed moving as in this code:

    private function productImageUploader($id, $image, $altText, $uploadDir)
    {
    
        //$uploadDir = (!substr($uploadDir, "/")) ? $uploadDir."/" : $uploadDir;
        $image = $_FILES[$image];
    
        if(is_array($image['name']))
        {
    
            for($i=0; $i<sizeof($image['name']); ++$i)
            {
    
                if($image['error'][$i] != 4)
                {
    
                    $image['name'][$i] = str_replace(" ", "-", $image['name'][$i]);
    
                    $targetPath = $uploadDir.basename($image['name'][$i]);
    
                    if(!move_uploaded_file($image['tmp_name'][$i], $targetPath))
                    {
    
                        switch($image['error'][$i])
                        {
    
                            case 1:
                                return "<p class=\"error\">The file for Extra Image {$i} is too large for the server.</p>";
                                break;
    
                            case 2:
                                return "<p class=\"error\">The file for Extra Image {$i} is too large for the form.</p>";
                                break;
    
                            case 3:                             
                            case 6:
                            case 7:
                                return "<p class=\"error\">An error occured uploading Extra Image {$i}. Please <a onclick=\"location.reload(true);\">refresh</a> the page.</p>";
                                break;  
    
                            case 8:
                                return "<p class=\"error\">The file for Extra Image {$i} is not the correct file type. Please convert it to either a jpg, .png or .bmp file.</p>";
                                break;
    
                        }
    
                    }
                    else
                    {
    
                        $thisAlt = (isset($_POST[$altText][$i]) && $_POST[$altText][$i] != "") ? $_POST[$altText][$i] : $_POST['prod_title'];
    
                        $data = array (
                            'pei_product_id' => $id,
                            'pei_location' => $image['name'][$i],
                            'pei_alt' => $thisAlt
                        );
    
                        if(!isset($_POST["pei_id"][$i]))
                        {
    
                            $this->db->insert("product_extra_images", $data);
    
                        }
                        else
                        {
    
                            $this->db->where("pei_id", $_POST['pei_id'][$i]);
                            $this->db->update("product_extra_images", $data);
    
                        }
    
                    }
    
                }
    
            }
    
            return true;  //This is the moved return
    
        }
        else
        {
    
            if($image['error'] != 4)
            {
    
                $image['name'] = str_replace(" ", "-", $image['name']);
    
                $targetPath = $uploadDir.basename($image['name']);
    
                if(!move_uploaded_file($image['tmp_name'], $targetPath))
                {
    
                    switch($image['error'])
                    {
    
                        case 1:
                            return "<p class=\"error\">The file for the main product image is too large for the server.</p>";
                            break;
    
                        case 2:
                            return "<p class=\"error\">The file for the main product image is too large for the form.</p>";
                            break;
    
                        case 3:                             
                        case 6:
                        case 7:
                            return "<p class=\"error\">An error occured uploading the main product image. Please <a onclick=\"location.reload(true);\">refresh the page.</p>";
                            break;  
    
                        case 8:
                            return "<p class=\"error\">The file for the main product image is not the correct file type. Please convert it to either a jpg, .png or .bmp file.</p>";
                            break;
    
                    }
    
                }
                else
                {
    
                    $data = array ('prod_main_image' => $image['name']);
    
                    $this->db->where("prod_id", $id);
                    $this->db->update("products", $data);
    
                    return true;
    
                }
    
            }
    
        }
    
        if(isset($_POST["pei_id"]) && is_array($_POST["pei_id"]))
        {
    
            for($i=0; $i<sizeof($_POST["pei_id"]); ++$i)
            {
    
                $thisAlt = (isset($_POST[$altText][$i]) && $_POST[$altText][$i] != "") ? $_POST[$altText][$i] : $_POST['prod_title'];
                $this->db->where("pei_id", $_POST["pei_id"][$i]);
                $this->db->update("product_extra_images", array("pei_alt"=> $thisAlt));
            }
    
        }
    
        return true;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a win form that creates a site in IIS7. One function needs
hai I have a magento web site. In this the search function compare with
I have a function that searches a database table via ajax. This is the
I have the following JavaScript in my site : $(function() { var $cells =
I have site which uses $(selector).load(path) function in more than 300 pages. Now my
I have created a signup page on my site, but the javascript validation function
I have function like this: function ypg_delete_img($id, $img) { $q = $this->ypg_get_one($id); $imgs =
I have function LoadTempMovieList(), and need to load movies from sessionStorage. But it seems
I have developed a small web app. This app allows users to upload images.
I am working on a site here: mfm.treethink.net All the jquery works fine in

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.