3

私のサイトには、$_FILES 配列から画像をアップロードする機能があります。この関数は、画像が 1 つしかなく、配列内の項目が 2 つ以下の場合にうまく機能しますが、アップロードできる画像は 2 つだけです。たとえば、私のフォームでは pei_name[] フィールドに最大 17 個の画像を許可していますが、最初の 2 つのフィールドしかアップロードできません。関数は次のとおりです。

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 配列の内容は次のとおりです。

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
            )

    )

)

4

2 に答える 2

2

最初return trueは間違った場所にありました。このコードのように移動する必要がありました:

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;

}
于 2012-09-10T10:36:28.570 に答える
1

ここで複数の画像をアップロードします.PHPはあなたが調べるかもしれないものです. これはあなたに役立つと思います。

于 2012-09-10T10:17:43.870 に答える