I am new in php and I am trying to make a multiple image uploader. My form looks like this:
<form action="" method="post" enctype="multipart/form-data">
<p class="style2">Izberi datoteko: <br /><input type="file" name="image" multiple="multiple" /></p>
<p class="style2">
Izberi Album: <br />
<select name="album_id">
<?php
foreach ($albums as $album) {
echo '<option value="', $album['id'] ,'">', $album['name'] ,'</option>';
}
?>
</select>
</p>
<p class="style2"><input type="submit" value="Naloži Slike" /></p>
</form>
And on the php side I did something like:
if (isset($_FILES['image'], $_POST['album_id'])) {
$image_name = $_FILES['image']['name'];
$image_size = $_FILES['image']['size'];
$image_temp = $_FILES['image']['tmp_name'];
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$a = explode('.', $image_name);
$image_ext = strtolower(end($a));
$album_id = $_POST['album_id'];
$errors = array();
if (empty($image_name) || empty($album_id)) {
$errors[] = '<p class="style2">Nekaj manjka!</p>';
} else {
if (in_array($image_ext, $allowed_ext) === false) {
$errors[] = '<p class="style2">Izbrani tip datoteke ni dovoljen!</p>';
}
if ($image_size > 2097152) {
$errors[] = '<p class="style2">Izbrana datoteka je prevelika! Maksimalna velikost datoteke mora biti 2mb!</p>';
}
}
if (!empty($errors)) {
foreach ($errors as $error) {
echo $error;
}
} else {
upload_image($image_temp, $image_ext, $album_id);
header('Location: view_album.php?album_id='.$album_id);
exit();
}
}
I created a function that puts the image into database. This function looks like this:
function upload_image($image_temp, $image_ext, $album_id) {
$album_id = (int)$album_id;
mysql_query("INSERT INTO `images` VALUES ('', '". $_SESSION['user_id'] ."', '$album_id', UNIX_TIMESTAMP(), '$image_ext')");
$image_id = mysql_insert_id();
$image_file = $image_id.'.'.$image_ext;
move_uploaded_file($image_temp, 'galerija/'.$album_id.'/'.$image_file);
create_thumb('galerija/'.$album_id.'/', $image_file, 'galerija/thumbs/'.$album_id.'/');
}
This works fine for uploading only one image. I tried to create a form for multiple select files and i can select more images but in database only gets first image. I tried to put foreach() in my script but I don t know where and how I must put it in.
So how can i change my script so I can upload more images into my database? I just can’t figure it out. Any help would be great!
A. PHP would not be albe to see your multiple files like that
Replace
With
To loop over this files you need something like