For循环没有给出预期的重复
Posted
技术标签:
【中文标题】For循环没有给出预期的重复【英文标题】:For loop not giving the expected repetitions 【发布时间】:2012-08-31 07:55:22 【问题描述】:我的站点中有一个函数可以从 $_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
)
)
)
【问题讨论】:
也许 html 表单也可以分享? 我已经添加了 $_FILES 数组,因为表单开始时没有额外图像的字段,但通过 javascript 最多可以添加 17 个。 你说的有点矛盾。但是,基于不查看您的代码,可能是upload_max_filesize
或 post_max_size
是罪魁祸首。
@iccthedral 不是这些,因为我上传的文件太小了。
您能否详细说明您对该问题所做的更改以及以前有效的答案不再适用的方式?
【参考方案1】:
第一个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;
【讨论】:
【参考方案2】:Upload multiple images, PHP 是您可能会研究的内容。我想这会对你有所帮助。
【讨论】:
以上是关于For循环没有给出预期的重复的主要内容,如果未能解决你的问题,请参考以下文章
带有指针值的 Go 通道在 for 循环中的行为不符合预期[重复]
如何在Python中的for循环内创建对象列表而不继承属性值[重复]
使用Python for循环,我怎样才能每次都从一个字符串中取出一个字母而没有任何重复