Php foreach循环,带有循环错误消息

时间:2009-10-12 01:52:21

标签: php error-handling loops foreach

我有一个foreach循环,可以检查项目是否选中了复选框。如果选中该复选框,我需要查看脚本的文件上载部分是否有值,如果是,则继续检查用户尝试上载的文件的文件类型和大小。

目前,如果第一个复选框只有一个错误,那么这样可以正常工作但由于某种原因,如果有多个文件出错,它将再次循环结果。例如,如果两个文件出现错误,它将给出四个结果(列出每个错误两次)如果有三个项目的错误我得到6个结果。

我对循环很新,我想知道是否应该切换到for循环并执行类似循环的操作,直到错误等于零。

这是我的HTML

<ul class="imgPreview">
    <li>
        <a href="#">dialogwarning_pimg4_15_1276648623.png</a> 
        <a href="#" class="edit_project_file"> <img src="images/edit.gif"/></a>
    </li>
    <li>
        <input name="upload_project_images[]" type="file" /> 
    </li>
    <li>
        <input name="edit_image[]" type="checkbox" value="dialogwarning_pimg4_15_1276648623.png"/>
    </li>
    <li>
        <a href="#">formatjustifyright_pimg3_15_1276648623.png</a>
    </li>
    <li class="edit_project_image">
        <input name="upload_project_images[]" type="file" />
    </li>
        <li class ="edit_file_checkbox">
        <input name="edit_image[]" type="checkbox" value="formatjustifyright_pimg3_15_1276648623.png"/>
    </li>
</ul>

PHP for循环如下:

$remove_images_files = $_POST['edit_image'];    

if(isset($remove_images_files)){
    foreach ($remove_images_files as $update){
        foreach ($_FILES["upload_project_images"]["type"] as $key => $type) {
        $upload_project_images = $_FILES["upload_project_images"]["name"][$key];
        $upload_project_images_type = $_FILES["upload_project_images"]["type"][$key];
        $upload_project_images_size = $_FILES["upload_project_images"]["size"][$key];
        $upload_project_image_ext = strtolower(substr($upload_project_images, strrpos($upload_project_images, '.') + 1));
            if($upload_project_images != "") {
                if((!in_array($upload_project_images_type,$upload_permitted_types['mime'])) || (!in_array($upload_project_image_ext,$upload_permitted_types['ext']))) {
                    $errmsg_arr[] = 'Please select a jpg, jpeg, gif, or png image to use as the project image' .$upload_project_images . $upload_project_image_ext;
                    $errflag = true;
                }
                elseif($upload_project_images_size > $max_upload_size_bytes ) {
                    $errmsg_arr[] = 'Please select an image smaller than '. $max_upload_size_Kbytes .' to use as the project image';
                    $errflag = true;
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

你的问题可能是你在迭代

$_FILES["upload_project_images"]["type"]

对于$remove_images_files中的每个条目,我认为这不是您想要做的。如果你拿出以下一行:

foreach ($remove_images_files as $update){

及其对应的},它能做你想做的事吗?