2

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!

1
  • Could you display the exact error you are receiving please. Commented Oct 14, 2012 at 21:51

4 Answers 4

3

A. PHP would not be albe to see your multiple files like that

Replace

  <input type="file" name="image" multiple="multiple" />

With

 <input type="file" name="image[]" multiple="multiple" />

To loop over this files you need something like

foreach ( $_FILES['image']['tmp_name'] as $key => $val ) {

    $fileName = $_FILES['image']['name'][$key];
    $fileSize = $_FILES['image']['size'][$key];
    $fileTemp = $_FILES['image']['tmp_name'][$key];

    $fileExt = pathinfo($fileName, PATHINFO_EXTENSION);
    $fileExt = strtolower($fileExt);

    // Continue
}
Sign up to request clarification or add additional context in comments.

6 Comments

I know that I need to do a loop over the files but evrythink I do in returs an error witch I don t know to fix. When I do what you suggested in returnes two errors: Undefined index: image AND Invalid argument supplied for foreach() I tryed to fix it but nothing helps.
What server are you using ??? it its not Apache you might have issues with name="image[]" multiple="multiple"
I figured it out why it returns me this two errors and solved that. Now it doesn t return me any errors and looks like everythink is ok but still uploads just one image. I can t figure that out. Do you know what am I doing wrong? If I print_r($_files) it returns me an array and gives me information for all selected images.
I would need to see your full code and test first ..... upu can add it to pastbin
I figured it out. After header() I had to erase exit() tag. Now it works perfectly. I almost killed my self whit this. Thank you for helping me!!!
|
2

you can find this helpful:

<?php
$target = "images/".$_FILES['image_upload']['name'];
$image_upload = mysql_real_escape_string(($_FILES['image_upload']['name']));
move_uploaded_file($_FILES['image_upload']['tmp_name'], $target);
for($i=2;$i<5;$i++)
{
if(!empty($_FILES['image_upload'.$i])):
$target = "images/".$_FILES['image_upload'.$i]['name'];
$image_upload.= ",".mysql_real_escape_string(($_FILES['image_upload'.$i]['name']));
 move_uploaded_file($_FILES['image_upload'.$i]['tmp_name'], $target);
 endif;
 }
 mysql_query("INSERT INTO `images` VALUES ('', '". $_SESSION['user_id'] ."', '$album_id', UNIX_TIMESTAMP(), '$image_upload')");
 ?>

make a folder in your root by using ftp. named it as "images". all images will be uploaded to this folder. change name of input type as per of yours.

happy coding!!

Comments

0

Change your name="image" to name="image[]"

example:

<pre><?php print_r($_FILES); ?></pre>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image[]" multiple="multiple" />
<input type="submit">
</form>

Comments

0

You received to a database one image because you sent only one image (not in a foreach loop), failed:

upload_image($image_temp, $image_ext, $album_id);

You should in HTML form add array of uploaded images such as:

<input type="file" name"image[]" multiple="multiple" />
<input type="file" name"image[]" multiple="multiple" />

And receives a loop of images:

if (isset($_POST["image"]))
{
    $album_id = (int)$_POST['album_id'];

    foreach ($_FILES["image"] as $images)
    {

        $image_temp = $images['tmp_name'];
        $image_name = $images['name'];

        $allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
        $a = explode('.', $image_name);
        $image_ext = strtolower(end($a));


        upload_image($image_temp, $image_ext, $album_id);
    }
}

If you are not dump it with var_dump($_FILES) for checking of array in a HTML form.

Notice you are also of victim SQL injection! Read in Stackoverflow about SQL injections.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.