Jump to content

foreach not working


Recommended Posts

Hi everyone,

 

i hsve a multiple image upload which works fine but isnt entering the file name into the database correctly, its only inputting the first filename but for all the records, can anybody see a problem with my code?

 

foreach($_FILES as $files => $_file){
$_POST[$files]="";
if($_file['name']!=""){
$pathinfo=pathinfo($_file['name']);
$file_name_array = explode(".", basename($_file['name']));
$filename = $file_name_array[count($file_name_array)-2];
$_POST[$files]=$file_prefix.$filename.$file_suffix.$date_stamp.".".$pathinfo['extension']; 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "multipleupload")) {
  $insertSQL = sprintf("INSERT INTO model_pictures (user_id, user_picture) VALUES (%s, %s)",
                       GetSQLValueString($_POST['userid'], "text"),
                       GetSQLValueString($_POST['file'], "text"));

  mysql_select_db($database_saucy_connection, $saucy_connection);
  $Result1 = mysql_query($insertSQL, $saucy_connection) or die(mysql_error());
}

}
}

Link to comment
https://forums.phpfreaks.com/topic/203150-foreach-not-working/
Share on other sites

Don't quite understand what's going on with your code, so I'll suggest a cleaner way, if I may. ;)

 

You should give all your upload fields the same name, such as "image[]". This'll result in an easy to use multi-dimensional array that we can easily loop through.

 

foreach ($_FILES as $key => $value) {
}

 

You'd access to each element in the array by using $value as it looped through $_FILES['image'][0] etc.

 

For example, on the second loop, you'd access the file name for $_FILES['image'][1] through $value['name'].

 

I was going to provide a detailed example, but I found your code a bit difficult to follow without sitting down and studying it, so I'm at odds at what it is exactly it's trying to do.

Link to comment
https://forums.phpfreaks.com/topic/203150-foreach-not-working/#findComment-1064421
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.