Here is the code I’m using to upload Multiple Photos with the HTML5 tag.
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){
$rand_file_name = rand(1,1000000);
if ($_FILES['files']['type'][$key] == "image/jpeg" || $_FILES['files']['type'][$key] == "image/gif" || $_FILES['files']['type'][$key] == "image/png" || $_FILES['files']['type'][$key] == "image/JPEG" || $_FILES['files']['type'][$key] == "image/GIF" || $_FILES['files']['type'][$key] == "image/PNG") {
if ($_FILES['files']['size'][$key] < 512000){
move_uploaded_file($tmp_name, "../../../assets/users/$user_id/photos/$browser_album_id/$rand_file_name.jpg");
$photo_name = $_FILES['files']['name'][$key];
$photo_name = str_replace(".jpg", "", $photo_name);
$photo_name = str_replace(".png", "", $photo_name);
$photo_name = str_replace(".gif", "", $photo_name);
$insert_database = mysql_query("INSERT INTO photo_album_photos (random_photo_name,belonging_album_id,photo_name) VALUES ('$rand_file_name','$browser_album_id','$photo_name')");
$num_files_added++;
}
}
}
The $num_files_added will count how many files were added.
what I would like to achieve is to gather the $rand_file_name(s) into an array. Each image is given a random name, so one image could be 812532.jpg and the next maybe 246546.jpg.
I would like to insert the file names into the database an array. So the above examples would be in an array. e.g. array(812532,246546);
Of course, I don’t want anyone to code this straight up for me, just help and guidance! 🙂
Before the
foreach, introduce a variable$file_names = array();as a blank arrayand in the foreach loop, try this :
Then, you can access the array that will be stored in the
$file_namesvariable. 🙂To make them into a comma separated string
Introduce
$file_names = ''before the foreach loop.Then in the loop, do this :
And after closing the foreach loop, this :