首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >For循环没有给出预期的重复次数

For循环没有给出预期的重复次数
EN

Stack Overflow用户
提问于 2012-09-06 21:19:05
回答 2查看 286关注 0票数 3

我在我的网站上有一个从$_FILES数组上传图像的函数。如果您只有一个图像,并且数组中有两个或更少的项目,则此函数可以很好地工作,但它只能上传2个图像。例如,我的表单允许在pei_name[]字段中最多包含17个图像,但它只上传了前2个字段。下面是函数:

代码语言:javascript
复制
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;

}

下面是$_FILES数组的内容:

代码语言:javascript
复制
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
            )

    )

)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-10 18:36:29

第一个return true出现在错误的位置。它需要像下面的代码那样移动:

代码语言:javascript
复制
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;

}
票数 2
EN

Stack Overflow用户

发布于 2012-09-10 18:17:44

这里的Upload multiple images, PHP是你可能会研究的东西。我想这对你会有帮助的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12301042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档