Using a foreach loop with PHP
<p>I am trying to loop through multiple uploaded files, from an HTML5 multiple file-uploader. I am using a foreach loop, but for some reason, it is not iterating through my files correctly. This foreach loop should do 1 of 4 things. </p>

<p>1) If the file size is bigger than 500kb, and a jpeg, it re-sizes the photo and uploads it to the images directory and adds the record to the database.</p>

<p>2) If the file is bigger than 500kb, and a png, it converts the photo to a jpeg, re-sizes it and uploads it to the images directory and adds the record to the database. (NOTE: my image resizing script only handles jpegs).</p>

<p>3) If the File is bigger than 5MB skip the photo and continue to the next loop.</p>

<p>3) If the file is smaller than 500kb, it uploads the photo to the images directory and adds the record to the database.</p>

<p>Now, I can get the image re-sizing to work without a problem, but for photos that are png's or photos that are bigger than 5MB I have a problem. For some reason, the loop only skips the photos that are greater than 5MB if they are the first element in the array, but not if they are in the middle or the end. Am I using the wrong method to call on and test my Array elements?</p>

<p>My PHP code:</p>

<pre><code>foreach ($_FILES['uploads']['name'] as $key =&gt; $file) {
    $time= time();

    // checking the photo's size
    if ($_FILES['uploads']['size'][$key] &gt; 500000 &amp;&amp; $_FILES['uploads']['size'][$key] &lt; 5242880) { 
        // check to make sure the file is a jpeg
        if ($_FILES['uploads']['type'][$key] == "image/jpeg") {
            // resizing the photo                                     
            include('resize_albums.php');
         }
         else if ($_FILES['uploads']['type'][$key] == "image/png") {
            // making the image a jpeg if it isn't already
            $image = imagecreatefrompng($_FILES['uploads']['tmp_name'][$key]);
            imagejpeg($image, $_FILES['uploads']['tmp_name'][$key], 100);
            // resizing the file
            include('resize_albums.php');
         }
    }
    else if ($_FILES['uploads']['size'][$key] &gt; 5242880) {
        continue;
    }
    else {
        // upload the photo and add the record to the database
    }
}
</code></pre>
