Foreach()

I have a form which allows users to upload up to 8 images


<input type="file" name="images[]" id="pic1" >
...
...

I then upload each of the files using


foreach ($images as $image) {

  $upload_exts = end(explode(".", $_FILES["images"][$image]["name"]));  

    if ((($_FILES["images"][$image]["type"] == "image/gif")  || ($_FILES["images"][$image]["type"] == "image/jpeg")  || ($_FILES["images"][$image]["type"] == "image/png")  || ($_FILES["images"][$image]["type"] == "image/pjpeg"))  && ($_FILES["images"][$image]["size"] < 2000000)  && in_array($upload_exts, $file_exts))  {  
        
      if ($_FILES["image1"]["error"] > 0)  
      {  
      echo "Return Code: " . $_FILES["file"]["error"] . "<br>";  
      }  else  {  
      echo "Upload: " . $_FILES["images"][$image]["name"] . "<br>";  
      echo "Type: " . $_FILES["images"][$image]["type"] . "<br>";  
      echo "Size: " . ($_FILES["images"][$image]["size"] / 1024) . " kB<br>";  
      echo "Temp file: " . $_FILES["images"][$image]["tmp_name"] . "<br>"; 
      // Enter your path to upload file here  
        if (file_exists("c:\\wamp\\www\\masterasp/providers/{$dbh->lastInsertId()}/" .$_FILES["image1"]["name"]))  
        {  echo "<div class='error'>"."(".$_FILES["images"][$image]["name"].")".  " already exists. "."</div>";   
        }  else  {  
        move_uploaded_file($_FILES["images"][$image]["tmp_name"],  "c:\\wamp\\www\\masterasp/providers/{$dbh->lastInsertId()}/" . $_FILES["imagea"][$image]["name"]);  
        echo "<div class='sucess'>"."Stored in: " .  "c:\\wamp\\www\\masterasp/providers/{$dbh->lastInsertId()}/" . $_FILES["images"][$image]["name"]."</div>";  
        }  
      }  
    }  else  {  
    echo "<div class='error'>Invalid file</div>";  
    }
}

I’m getting,

[TABLE=“class: xdebug-error xe-notice”]
[TR]
[TH=“bgcolor: #f57900, colspan: 5, align: left”]COLOR=#fce94f[/COLOR] Notice: Undefined variable: images in C:\wamp\www\masterasp\admin\insert_provider.php on line 105[/TH]
[/TR]
[TR]
[TH=“bgcolor: #e9b96e, colspan: 5, align: left”]Call Stack[/TH]
[/TR]
[TR]
[TH=“bgcolor: #eeeeec, align: center”]#[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Time[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Memory[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Function[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Location[/TH]
[/TR]
[TR]
[TD=“bgcolor: #eeeeec, align: center”]1[/TD]
[TD=“bgcolor: #eeeeec, align: center”]0.0010[/TD]
[TD=“bgcolor: #eeeeec, align: right”]282024[/TD]
[TD=“bgcolor: #eeeeec”]{main}( )[/TD]
[TD=“bgcolor: #eeeeec”]…\insert_provider.php:0[/TD]
[/TR]
[/TABLE]

[TABLE=“class: xdebug-error xe-warning”]
[TR]
[TH=“bgcolor: #f57900, colspan: 5, align: left”]COLOR=#fce94f[/COLOR] Warning: Invalid argument supplied for foreach() in C:\wamp\www\masterasp\admin\insert_provider.php on line 105[/TH]
[/TR]
[TR]
[TH=“bgcolor: #e9b96e, colspan: 5, align: left”]Call Stack[/TH]
[/TR]
[TR]
[TH=“bgcolor: #eeeeec, align: center”]#[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Time[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Memory[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Function[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Location[/TH]
[/TR]
[TR]
[TD=“bgcolor: #eeeeec, align: center”]1[/TD]
[TD=“bgcolor: #eeeeec, align: center”]0.0010[/TD]
[TD=“bgcolor: #eeeeec, align: right”]282024[/TD]
[TD=“bgcolor: #eeeeec”]{main}( )[/TD]
[TD=“bgcolor: #eeeeec”]…\insert_provider.php:0[/TD]
[/TR]
[/TABLE]

Immediately before your for each() loop try this.



echo '<pre>';
print_r( $_POST );
echo '</pre>';
die;


That printed the _POST variables (none). The file uploads are in the _FILES so when I did


var_dump($_FILES);

I get


<b>array</b> <i>(size=1)</i>
  'images' <font color="#888a85">=></font> 
    <b>array</b> <i>(size=5)</i>
      'name' <font color="#888a85">=></font> 
        <b>array</b> <i>(size=8)</i>
          0 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">'mysql_error.png'</font> <i>(length=15)</i>
          1 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">''</font> <i>(length=0)</i>
          2 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">''</font> <i>(length=0)</i>
          3 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">'slider4.jpg'</font> <i>(length=11)</i>
          4 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">''</font> <i>(length=0)</i>
          5 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">''</font> <i>(length=0)</i>
          6 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">''</font> <i>(length=0)</i>
          7 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">''</font> <i>(length=0)</i>
      'type' <font color="#888a85">=></font> 
        <b>array</b> <i>(size=8)</i>
          0 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">'image/png'</font> <i>(length=9)</i>
          1 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">''</font> <i>(length=0)</i>
          2 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">''</font> <i>(length=0)</i>
          3 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">'image/jpeg'</font> <i>(length=10)</i>
          4 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">''</font> <i>(length=0)</i>
          5 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">''</font> <i>(length=0)</i>
          6 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">''</font> <i>(length=0)</i>
          7 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">''</font> <i>(length=0)</i>
      'tmp_name' <font color="#888a85">=></font> 
        <b>array</b> <i>(size=8)</i>
          0 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">'C:\\wamp\	mp\\php30.tmp'</font> <i>(length=21)</i>
          1 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">''</font> <i>(length=0)</i>
          2 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">''</font> <i>(length=0)</i>
          3 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">'C:\\wamp\	mp\\php60.tmp'</font> <i>(length=21)</i>
          4 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">''</font> <i>(length=0)</i>
          5 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">''</font> <i>(length=0)</i>
          6 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">''</font> <i>(length=0)</i>
          7 <font color="#888a85">=></font> <small>string</small> <font color="#cc0000">''</font> <i>(length=0)</i>
      'error' <font color="#888a85">=></font> 
        <b>array</b> <i>(size=8)</i>
          0 <font color="#888a85">=></font> <small>int</small> <font color="#4e9a06">0</font>
          1 <font color="#888a85">=></font> <small>int</small> <font color="#4e9a06">4</font>
          2 <font color="#888a85">=></font> <small>int</small> <font color="#4e9a06">4</font>
          3 <font color="#888a85">=></font> <small>int</small> <font color="#4e9a06">0</font>
          4 <font color="#888a85">=></font> <small>int</small> <font color="#4e9a06">4</font>
          5 <font color="#888a85">=></font> <small>int</small> <font color="#4e9a06">4</font>
          6 <font color="#888a85">=></font> <small>int</small> <font color="#4e9a06">4</font>
          7 <font color="#888a85">=></font> <small>int</small> <font color="#4e9a06">4</font>
      'size' <font color="#888a85">=></font> 
        <b>array</b> <i>(size=8)</i>
          0 <font color="#888a85">=></font> <small>int</small> <font color="#4e9a06">140879</font>
          1 <font color="#888a85">=></font> <small>int</small> <font color="#4e9a06">0</font>
          2 <font color="#888a85">=></font> <small>int</small> <font color="#4e9a06">0</font>
          3 <font color="#888a85">=></font> <small>int</small> <font color="#4e9a06">75850</font>
          4 <font color="#888a85">=></font> <small>int</small> <font color="#4e9a06">0</font>
          5 <font color="#888a85">=></font> <small>int</small> <font color="#4e9a06">0</font>
          6 <font color="#888a85">=></font> <small>int</small> <font color="#4e9a06">0</font>
          7 <font color="#888a85">=></font> <small>int</small> <font color="#4e9a06">0</font>

I would have thought that the $_POST[‘images’] should be set.

I think the problem could be in the <form> statement. Is it possible to see the complete file?

You could also try $images = $_FILES[‘images’] just before the foreach() loop.

it looks like the $image variable is supposed to be an array index, so foreach() would be the wrong structure here. either use a for() loop (counting from 0 to count()) or, when using foreach(), use $image instead of $_FILES[“images”][$image] (and then run the foreach() on $_FILES[“images”] instead on relying on register_globals)

heres the form


<form method="post" action="insert_provider.php" class="form-horizontal" role="form" enctype="multipart/form-data"><div class="form-group">	<label for="pic1" class="col-sm-2 control-label">Image 1</label>    <div class="col-sm-4">    <input type="file" name="images[]" id="pic1" >    </div>    <label for="pic2" class="col-sm-2 control-label">Image 2</label>    <div class="col-sm-4">	<input type="file" name="images[]" id="pic2">	</div></div><div class="form-group">	<label for="pic3" class="col-sm-2 control-label">Image 3</label>    <div class="col-sm-4">	<input type="file" name="images[]" id="pic3">	</div>	<label for="pic4" class="col-sm-2 control-label">Image 4</label>    <div class="col-sm-4">	<input type="file" name="images[]" id="pic4" >	</div></div><div class="form-group">	<label for="pic5" class="col-sm-2 control-label">Image 5</label>    <div class="col-sm-4">	<input type="file" name="images[]" id="pic5" >	</div>	<label for="pic6" class="col-sm-2 control-label">Image 6</label>    <div class="col-sm-4">	<input type="file" name="images[]" id="pic6">	</div></div><div class="form-group">	<label for="pic7" class="col-sm-2 control-label">Image 7</label>    <div class="col-sm-4">	<input type="file" name="images[]" id="pic7">	</div>	<label for="pic8" class="col-sm-2 control-label">Image 8</label>    <div class="col-sm-4">	<input type="file" name="images[]"  id="pic8">	</div></div><div class="form-group">    <button type="submit" class="btn btn-primary col-sm-offset-2">      <span class="glyphicon glyphicon-plus"></span>&nbsp;&nbsp;Add Provider    </button></div><input type="hidden" name="date_created" value="<?=date('Y-m-d')?>"></form>

I changed the code to…


$images = $_FILES['images'];


for ( $counter = 0; $counter < count($images); $counter++){


$upload_exts = end(explode(".", $_FILES["images"][$counter]["name"]));
  if ((($_FILES["images"][$counter]["type"] == "image/gif")
  || ($_FILES["images"][$counter]["type"] == "image/jpeg")
  || ($_FILES["images"][$counter]["type"] == "image/png")
  || ($_FILES["images"][$counter]["type"] == "image/pjpeg"))
  && ($_FILES["images"][$counter]["size"] < 2000000)
  && in_array($upload_exts, $file_exts))
  {
  if ($_FILES["images"][$counter]["error"] > 0)
  {
  echo "Return Code: " . $_FILES["images"][$counter]["error"] . "<br>";
  }
  else
  {
  echo "Upload: " . $_FILES["images"][$counter]["name"] . "<br>";
  echo "Type: " . $_FILES["images"][$counter]["type"] . "<br>";
  echo "Size: " . ($_FILES["images"][$counter]["size"] / 1024) . " kB<br>";
  echo "Temp file: " . $_FILES["images"][$counter]["tmp_name"] . "<br>";
  // Enter your path to upload file here
  if (file_exists("c:\\wamp\\www\\masterasp/providers/{$dbh->lastInsertId()}/" .$_FILES["images"][$counter]["name"]))
  {
  echo "<div class='error'>"."(".$_FILES["images"][$counter]["name"].")".
  " already exists. "."</div>";
  }
  else
  {
  move_uploaded_file($_FILES["images"][$counter]["tmp_name"],
  "c:\\wamp\\www\\masterasp/providers/{$dbh->lastInsertId()}/" . $_FILES["imagea"][$counter]["name"]);
  echo "<div class='sucess'>"."Stored in: " .
  "c:\\wamp\\www\\masterasp/providers/{$dbh->lastInsertId()}/" . $_FILES["images"][$counter]["name"]."</div>";
  }
  }
  }
  else
  {
  echo "<div class='error'>Invalid file</div>";
  }
}


and am getting lads of warnings, heres the top 3

[TABLE=“class: xdebug-error xe-notice”]
[TR]
[TH=“bgcolor: #f57900, colspan: 5, align: left”]COLOR=#fce94f[/COLOR] Notice: Undefined offset: 0 in C:\wamp\www\masterasp\admin\insert_provider.php on line 109[/TH]
[/TR]
[TR]
[TH=“bgcolor: #e9b96e, colspan: 5, align: left”]Call Stack[/TH]
[/TR]
[TR]
[TH=“bgcolor: #eeeeec, align: center”]#[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Time[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Memory[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Function[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Location[/TH]
[/TR]
[TR]
[TD=“bgcolor: #eeeeec, align: center”]1[/TD]
[TD=“bgcolor: #eeeeec, align: center”]0.0010[/TD]
[TD=“bgcolor: #eeeeec, align: right”]283120[/TD]
[TD=“bgcolor: #eeeeec”]{main}( )[/TD]
[TD=“bgcolor: #eeeeec”]…\insert_provider.php:0[/TD]
[/TR]
[/TABLE]

[TABLE=“class: xdebug-error xe-strict-standards”]
[TR]
[TH=“bgcolor: #f57900, colspan: 5, align: left”]COLOR=#fce94f[/COLOR] Strict standards: Only variables should be passed by reference in C:\wamp\www\masterasp\admin\insert_provider.php on line 109[/TH]
[/TR]
[TR]
[TH=“bgcolor: #e9b96e, colspan: 5, align: left”]Call Stack[/TH]
[/TR]
[TR]
[TH=“bgcolor: #eeeeec, align: center”]#[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Time[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Memory[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Function[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Location[/TH]
[/TR]
[TR]
[TD=“bgcolor: #eeeeec, align: center”]1[/TD]
[TD=“bgcolor: #eeeeec, align: center”]0.0010[/TD]
[TD=“bgcolor: #eeeeec, align: right”]283120[/TD]
[TD=“bgcolor: #eeeeec”]{main}( )[/TD]
[TD=“bgcolor: #eeeeec”]…\insert_provider.php:0[/TD]
[/TR]
[/TABLE]

[TABLE=“class: xdebug-error xe-notice”]
[TR]
[TH=“bgcolor: #f57900, colspan: 5, align: left”]COLOR=#fce94f[/COLOR] Notice: Undefined offset: 0 in C:\wamp\www\masterasp\admin\insert_provider.php on line 110[/TH]
[/TR]
[TR]
[TH=“bgcolor: #e9b96e, colspan: 5, align: left”]Call Stack[/TH]
[/TR]
[TR]
[TH=“bgcolor: #eeeeec, align: center”]#[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Time[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Memory[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Function[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Location[/TH]
[/TR]
[TR]
[TD=“bgcolor: #eeeeec, align: center”]1[/TD]
[TD=“bgcolor: #eeeeec, align: center”]0.0010[/TD]
[TD=“bgcolor: #eeeeec, align: right”]283120[/TD]
[TD=“bgcolor: #eeeeec”]{main}( )[/TD]
[TD=“bgcolor: #eeeeec”]…\insert_provider.php:0[/TD]
[/TR]
[/TABLE]

I have uploaded your HTML input with slight modifications and also created a table of the image files parameters.

http://www.johns-jokes.com/downloads/sp-c/lukeurtnowski_files/index.php

Source file details are at the bottom of the file.

I will leave you to extract the relevant PHP file details into your own source code :slight_smile:

thanks a lot!

Wow, thats soo cool, is it ok if I use this?

No problems, please feel free to use the script.

ok, tried to change things around to check the extension and get,

[TABLE=“class: xdebug-error xe-strict-standards”]
[TR]
[TH=“bgcolor: #f57900, colspan: 5, align: left”]COLOR=#fce94f[/COLOR] Strict standards: Only variables should be passed by reference in C:\wamp\www\masterasp\admin\insert_provider.php on line 128[/TH]
[/TR]
[TR]
[TH=“bgcolor: #e9b96e, colspan: 5, align: left”]Call Stack[/TH]
[/TR]
[TR]
[TH=“bgcolor: #eeeeec, align: center”]#[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Time[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Memory[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Function[/TH]
[TH=“bgcolor: #eeeeec, align: left”]Location[/TH]
[/TR]
[TR]
[TD=“bgcolor: #eeeeec, align: center”]1[/TD]
[TD=“bgcolor: #eeeeec, align: center”]0.0000[/TD]
[TD=“bgcolor: #eeeeec, align: right”]283424[/TD]
[TD=“bgcolor: #eeeeec”]{main}( )[/TD]
[TD=“bgcolor: #eeeeec”]…\insert_provider.php:0[/TD]
[/TR]
[/TABLE]

heres the php code from, line 128, where I check the files extension


  $upload_exts = end(explode(".", $_FILES["images"]["name"][$counter]));


  if ((($_FILES["images"]["type"][$counter] == "image/gif")
  || ($_FILES["images"]["type"][$counter] == "image/jpeg")
  || ($_FILES["images"]["type"][$counter] == "image/png")
  || ($_FILES["images"]["type"][$counter] == "image/pjpeg"))
  && ($_FILES["images"]["size"][$counter] < 2000000)
  && in_array($upload_exts, $file_exts))