views:

308

answers:

1

Hello,

I have a foreach loop that is suppose to check to see if a checkbox is checked by an item. If the checkbox is checked I need to see if the file upload part of the script has a value and if so then proceed to check the file type and size of the file the user is trying to upload.

Currently if there is only an error with the first checkbox then this works fine but for some reason if there is an error with more than one file it will loop the results again. For example if there is an error with two files it will give four results (listing each error twice) if an error with three items I get 6 results.

I'm really new to loops and am wondering if I should switch to a for loop and do something like loop until errors equal zero.

Here is my 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>

The PHP for loop is as followed:

$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;
       }
      }
     }
    }
}
+2  A: 

Your problem may be that you are iterating over

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

for every entry in $remove_images_files, which I don't think is what you want to do. If you take out the following line:

foreach ($remove_images_files as $update){

and its corresponding }, does it do what you want it to do?

James McNellis
Weird, I could have sworn I tried that but that seamed to fix the problem so I must have done it wrong. Thanks for the help.
BandonRandon
You are most welcome.
James McNellis