0

I know this question is repeated over and over.. But can't seem to find solution for my problem...

How can I create a form which will allow user to upload xy images at once?

Here's my html code:

<form class="pure-form" action="upload.php" enctype="multipart/form-data" method="post">

        <div class="upload">
            <a onclick="select_file()" class="pure-button">Choose a Image</a>
            <input id="image" type="file" name="image[]" multiple="multiple">
        </div>

        <!--image preview-->
        <img src="" style="display:none">

        <input class="pure-button pure-button-primary" type="submit" value="Upload!">
    </form>

And here's my php code:

$valid_exts = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
$max_size = 2048 * 1024; // max file size (200kb)
$path = 'uploads/'; // upload directory

if ( $_SERVER['REQUEST_METHOD'] === 'POST' )
{
    if( @is_uploaded_file($_FILES['image']['tmp_name']) )
    {
        // get uploaded file extension
        $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
        // looking for format and size validity
        if (in_array($ext, $valid_exts) AND $_FILES['image']['size'] < $max_size)
        {
            // unique file path
            $path = $path . uniqid(). '.' .$ext;
            // move uploaded file from temp to uploads directory
            if (move_uploaded_file($_FILES['image']['tmp_name'], $path))
            {
                $status = 'Image uploaded successfully!';
                $status = $path;
            }
            else {
                $status = 'Upload Fail: Unknown error occurred!';
            }
        }
        else {
            $status = 'Upload Fail: Unsupported file format or It is too large to upload!';
        }
    }
    else {
        $status = 'Upload Fail: File not uploaded!';
    }
}
else {
    $status = 'Bad request!';
}

// echo out json encoded status
echo json_encode(array('status' => $status));
?>

Is it possible to solve this problem with foreach? If so, how should I do that?

2 Answers 2

1

Try this (add as many file fields as you want):

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

Php:

$valid_exts = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
$max_size = 2048 * 1024; // max file size (200kb)
$path = 'uploads/'; // upload directory

if(isset($_FILES['image'])){
    for($i=0; $i<count($_FILES['image']['name']); $i++){
        if( @is_uploaded_file($_FILES['image']['tmp_name'][$i]) )
            {
                // get uploaded file extension
                $ext = strtolower(pathinfo($_FILES['image']['name'][$i], PATHINFO_EXTENSION));
                // looking for format and size validity
                if (in_array($ext, $valid_exts) AND $_FILES['image']['size'][$i] < $max_size)
                {
                    // unique file path
                    $path = $path . uniqid(). '.' .$ext;
                    // move uploaded file from temp to uploads directory
                    if (move_uploaded_file($_FILES['image']['tmp_name'][$i], $path))
                    {
                        $status = 'Image uploaded successfully!';
                        $status = $path;
                    }
                    else {
                        $status = 'Upload Fail: Unknown error occurred!';
                    }
                }
                else {
                    $status = 'Upload Fail: Unsupported file format or It is too large to upload!';
                }
            }
            else {
                $status = 'Upload Fail: File not uploaded!';
            }
        echo "<p>$status</p>";
    }
}
?>
Sign up to request clarification or add additional context in comments.

5 Comments

ok i'll quickly whip up a working code which you can modify to your needs
When it echo's out the message.. Last filename is like this uploads/52a78bfc11470.jpg52a78bfc1155b.jpg52a78bfc1160f.jpg52a78bfc116f5.jpg This is too long.
Why did you have $status = $path; - for debugging? Maybe this line can be removed if it's working so you get the proper message or even better use $status = $_FILES['image']['name'][$i] . ' uploaded successfully!'; instead of $status = 'Image uploaded successfully!';
This script changes name of file which is uploaded.. And for the next file it first copies previous filename and adds new filename to current filename. So if image is abc.jpg the next image will be abc.jpgabcd.jpg
@user3002173 you can change the filename to anything you want by updating $path = $path . uniqid(). '.' .$ext; to use original filename (not recommended), you can use $path = $path . $_FILES['image']['name'][$i];
0
 <form class="pure-form" action="upload.php" enctype="multipart/form-data" method="post">

    <div class="upload">
        <a onclick="select_file()" class="pure-button">Choose a Image</a>
        <input id="image" type="file" name="image[1]" multiple="multiple">
<input id="image" type="file" name="image[2]" multiple="multiple">


    </div>

    <!--image preview-->
    <img src="" style="display:none">

    <input class="pure-button pure-button-primary" type="submit" value="Upload!">
   </form>

PHP

$valid_exts = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
$max_size = 2048 * 1024; // max file size (200kb)
$path = 'uploads/'; // upload directory

if ( $_SERVER['REQUEST_METHOD'] === 'POST' )
{
if( @is_uploaded_file($_FILES['image1']['tmp_name']) )
{
    // get uploaded file extension
    $ext = strtolower(pathinfo($_FILES['image1']['name'], PATHINFO_EXTENSION));
    // looking for format and size validity
    if (in_array($ext, $valid_exts) AND $_FILES['image1']['size'] < $max_size)
    {
        // unique file path
        $path = $path . uniqid(). '.' .$ext;
        // move uploaded file from temp to uploads directory
        if (move_uploaded_file($_FILES['image1']['tmp_name'], $path))



 if( @is_uploaded_file($_FILES['image2']['tmp_name']) )
  {
    // get uploaded file extension
    $ext = strtolower(pathinfo($_FILES['image2']['name'], PATHINFO_EXTENSION));
    // looking for format and size validity
    if (in_array($ext, $valid_exts) AND $_FILES['image2']['size'] < $max_size)
    {
        // unique file path
        $path = $path . uniqid(). '.' .$ext;
        // move uploaded file from temp to uploads directory
        if (move_uploaded_file($_FILES['image2']['tmp_name'], $path))
        {





            $status = 'Image uploaded successfully!';
            $status = $path;
        }
        else {
            $status = 'Upload Fail: Unknown error occurred!';
        }
       }
      else {
        $status = 'Upload Fail: Unsupported file format or It is too large to upload!';
    }
    }
    else {
    $status = 'Upload Fail: File not uploaded!';
    }
}
else {
$status = 'Bad request!';
}
}}
   // echo out json encoded status
   echo json_encode(array('status' => $status));

  ?>

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.