gpt4 book ai didi

php - For循环没有给出预期的重复

转载 作者:搜寻专家 更新时间:2023-10-31 20:46:47 25 4
gpt4 key购买 nike

我的站点中有一个函数可以从 $_FILES 数组上传图像。如果你只有一张图片,数组中有两个或更少的项目,这个函数就可以正常工作,但它只能上传 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
)

)

)

最佳答案

第一个 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;

}

关于php - For循环没有给出预期的重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12301042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com