0

Hi there I want to make a function for me to able upload a multiple image in one submission below are my code structure:

<form action="upload.php" method="post" enctype="multipart/form-data">
  <p>Image1 :<input name="image1" type="file" /></p>
  <p>Image2 :<input name="image2" type="file" /></p>
  <p>Image3 :<input name="image3" type="file" /></p>
  <input type="submit" value="Submit" />
</form>

include('configdb.php');

$uploadDir = 'upload/';

if(isset($_POST['submit']))
{
$fileName = $_FILES['image1']['name'];
$tmpName  = $_FILES['image1']['tmp_name'];
$fileSize = $_FILES['image1']['size'];
$fileType = $_FILES['image1']['type'];

$image1path = $uploadDir . $fileName;


$result = move_uploaded_file($tmpName, "$image1path");
if (!$result) {
echo "Error uploading";
exit;
}

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
    $image1 = addslashes($image1path);
}



$sql="INSERT INTO picture (image1, image2, image3) VALUES ('$image1','$image2', $image3)";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

}

Basically my code above does one image upload, How can I make a function to be able upload 3 images.

4
  • foreach ($_FILES as $name => $file) would be a start... Commented Nov 25, 2011 at 3:13
  • You're neglecting to check for upload errors and assuming everything worked fine. Bad way to go... very bad. Commented Nov 25, 2011 at 4:37
  • @MarcB I will add a validation after test upload of 3 images works fine :) If you can help I really appreciate it thanks :) Commented Nov 25, 2011 at 4:53
  • if ($_FILES['image1']['error'] !== UPLOAD_ERR_OK) { ... upload failed ... } Commented Nov 25, 2011 at 4:54

2 Answers 2

1

Here is the code to multiple upload image. You can upload more than 3 images.

<form action="upload.php" method="post" enctype="multipart/form-data">
  <p>Image1 :<input name="image[]" type="file" /></p>
  <p>Image2 :<input name="image[]" type="file" /></p>
  <p>Image3 :<input name="image[]" type="file" /></p>
  <input type="submit" name="submit" value="Submit" />
</form>

<?php
include('configdb.php');

$uploadDir = 'upload/';

if(isset($_POST['submit'])) {

    $image = array();

    foreach($_FILES['image']['name'] as $index => $name) {

        if($_FILES['image']['error'][$index] == 4) {
            continue;
        }

        if($_FILES['image']['error'][$index] == 0) {

            $fileName = $_FILES['image']['name'][$index];
            $tmpName  = $_FILES['image']['tmp_name'][$index];
            $fileSize = $_FILES['image']['size'][$index];
            $fileType = $_FILES['image']['type'][$index];

            if(($fileType == "image/gif"   ||
                $fileType == "image/jpeg"  ||
                $fileType == "image/pjpeg" ||
                $fileType == "image/png"   ||
                $fileType == "image/x-png") && 
                $fileSize < 500000) {

                $imagePath = $uploadDir . $fileName;

                $result = @move_uploaded_file($tmpName, $imagePath);
                if (!$result) {
                    echo "Error uploading";
                    exit;
                }
                $image[] = $imagePath;
            }
        }
    }
    // Save images to database
    $nbImage = count($image);
    if($nbImage) {

        $sql = "INSERT INTO picture (image1, image2, image3) VALUES (";
        for($i=0; $i<$nbImage; $i++) {
            if($i) $sql .= ",";
            $sql .= "\"".$image[$i]."\"";
        }
        $sql .= ")";

        @mysql_query($sql);
    }
}

?>

Note: you should test the type and the size of image before upload it because of the security.

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

4 Comments

I test the your code but it doesnt work it said undefind photo then I replace it with "image" like in form file filed. but it doesnt work :(
I just edit your form. You didn't add name="submit" why it doesn't work. :)
I copy your code and try it again but it doesnt work too, It doesnt save image in directory and in database
i already test it and it works well. you should check your permission upload directory. Also you can try to print error to fix your problem.
1

Try something like this:

...
$images = array();
foreach (array('image1', 'image2', 'image3') as $name) {
    $images[$name] = new stdClass();
    $images[$name]->fileName = $_FILES[$name]['name'];
    $images[$name]->tmpName  = $_FILES[$name]['tmp_name'];
    $images[$name]->fileSize = $_FILES[$name]['size'];
    $images[$name]->fileType = $_FILES[$name]['type'];

    $images[$name]->path = $uploadDir . $images[$name]->fileName;

    $result = move_uploaded_file($images[$name]->tmpName, $images[$name]->path);
    if (!$result) {
        echo "Error uploading";
        exit;
    }

    $images[$name]->sql = mysql_real_escape_string($images[$name]->path);
}

$sql="INSERT INTO picture (image1, image2, image3) VALUES ({$images['image1']},{$images['image2']},{$images['image3']})";
...

3 Comments

@Petah, it must be stdClass instead of stdObject
@Petah I recieve this error Fatal error: Class 'stdObject' not found in C: and when I comment out the //*$images[$name] = new stdObject(); and I recieve this error Strict Standards: Creating default object from empty value. thanks
@robert I change what you said and I receive Error: Column count doesn't match value count at row 1 and with this in line of sql statement Catchable fatal error: Object of class stdClass could not be converted to string in

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.