0

I'm trying to fill the empty array called $Errors with some items to make some restrictions on my uploading form. So I'm trying to put a <div> tag as an item inside the array and that div tag holds some strings and I want to concatenate these strings with a foreach loop as shown in the code below but it keeps giving an error and I don't actually know how to concatenate a function like foreach with strings.

$Errors= array();
$Ext= array('jpg','png','jpeg','bmp','gif');
$FileName= $_FILES ['UploadedFile']['name'];
$FileExt=strtolower(end(explode(".",$FileName)));

if(!in_array($FileExt,$Ext)):
$Errors[]="<div>The file that you're uploading is " . $FileName . " And it is not an image file, only these file types are supported " . foreach($Ext as $AllowedExt):echo $AllowedExt;
endforeach;"</div>";endif;
juliomalves
  • 42,130
  • 20
  • 150
  • 146
Ahmus
  • 3
  • 4

2 Answers2

0

Trying to loop over an array while your building a string is.. messy at best.

Build your string from the arrays content first and then concat that onto the rest of the string. You can also use implode to convert an array into a string.

$Errors= array();
$Ext= array('jpg','png','jpeg','bmp','gif');
$ExtAsString =  implode(",", $Ext);
$FileName= $_FILES ['UploadedFile']['name'];
$FileExt=strtolower(end(explode(".",$FileName)));

if(!in_array($FileExt,$Ext)):
    $Errors[]="<div>The file that you're uploading is " . $FileName . " And it is not an image file, only these file types are supported " . $ExtAsString . "</div>";
endif;
DevWithZachary
  • 3,545
  • 11
  • 49
  • 101
0

Write your foreach() statement as a separate function and concatenate it with your text. And besides the items in your allowed extension isn't that too many to written directly in the text.