$ _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 배열을 추가했습니다. 양식은 추가 이미지 용 입력란없이 시작되지만 자바 스크립트를 통해 최대 17 개까지 추가 할 수 있습니다. –
당신이 말하는 것은 약간 모순입니다. 그러나 코드를 보지 않았을 때,'upload_max_filesize' 또는'post_max_size'가 원인 일 수 있습니다. – nullpotent