PHP: Uploading multiple images at the same time

sparecycle

I've created an HTML form that allows me to upload an image to a uniquely named directory (i.e. a combination of date/time/ip address). Now I'm trying to upload two images (eventually multiple). I've reproduced my "File 1" content and uniquely named it to "File 2". I quickly attached a "2" to every variable and post field. Here is my upload form HTML:

<!DOCTYPE html>
<html>
<body>

<form action="icanhazuploads.php" method="post" enctype="multipart/form-data">
Select image(s) to upload: <br /><br />
<input type="file" name="fileToUpload" class="fileToUpload"><br /><br />
<input type="file" name="fileToUpload2" class="fileToUpload2"><br /><br />
<input type="submit" name="submit">
</form>

</body>
</html> 

Pretty straight forward. Two upload fields and a submit button. Here is my PHP processor file:

<?php
// Display Errors
ini_set('display_errors', 'On');
ini_set('html_errors', 0);

$ip = ip2long($_SERVER['REMOTE_ADDR']);
$datetime = date('YmdHis');  
$target_dir = "uploads/" . $datetime . $ip . "/";

// Create the unique diretory
if (!file_exists($target_dir)) {
    mkdir($target_dir, 0777, true);
}

// Start processing files

// File 1
echo "<br />1st file being added...<br />";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
echo "<br />Target File: " . $target_file. "<br />";
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".<br />";
        $uploadOk = 1;
    } else {
        echo "File is not an image.<br />";
        $uploadOk = 0;
    }
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Your file was not uploaded.<br />";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.<br />";
    } else {
        echo "There was an error uploading your file.<br />";
    }
}



// File 2
if(isset($_POST["fileToUpload2"])) {
echo "<br />2nd file being added...<br />";
$target_file2 = $target_dir . basename($_FILES["fileToUpload2"]["name"]);
echo "<br />Target File: " . $target_file2. "<br />";
$uploadOk = 1;
$imageFileType = pathinfo($target_file2,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload2"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".<br />";
        $uploadOk = 1;
    } else {
        echo "File is not an image.<br />";
        $uploadOk = 0;
    }
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Your file was not uploaded.<br />";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload2"]["tmp_name"], $target_file2)) {
        echo "The file ". basename( $_FILES["fileToUpload2"]["name"]). " has been uploaded.<br />";
    } else {
        echo "There was an error uploading your file.<br />";
    }
}

Truly magical. I pull up my HTML form in the browser, select two images and hit submit. I can watch with Firebug or Chrome's inspector and see it with the two image fields populated via POST. However, my PHP file will consistently only see one. If I put my image only in the second field in my form, it still just acts as if I placed it in the first field. No errors in error log. Any idea why it refuses to see more than one file?

My php.ini settings:

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 64M

; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20
rjdown

if(isset($_POST["fileToUpload2"])) {

This is wrong, it will never be set.

Try

if(isset($_FILES["fileToUpload2"])) {

For future debugging, use var_dump($_POST); and var_dump($_FILES); and you'll instantly see what's going on

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Multiple images rename & uploading in PHP

From Dev

Multiple images rename & uploading in PHP

From Dev

PHP: Uploading multiple images to imgur at once

From Dev

Uploading multiple images in webservice using PHP

From Dev

Mysql updating multiple textboxes and uploading multiple files at the same time

From Dev

Mysql updating multiple textboxes and uploading multiple files at the same time

From Dev

Uploading multiple images with volley?

From Dev

Uploading multiple images in codeigniter?

From Dev

Uploading multiple images with paperclip?

From Dev

Uploading multiple images with Django

From Dev

Uploading multiple images in codeigniter?

From Dev

Django multiple images not uploading

From Dev

Uploading multiple images one to each row PHP and Mysqli db

From Dev

Uploading Multiple Images using CarrierWave

From Dev

All images not uploading in multiple images upload

From Dev

Wont uploading bigger images PHP

From Dev

PHP: Images file is not uploading on the server?

From Dev

PHP foreach($images as $image) not uploading images

From Dev

Uploading multiple files in the same request

From Dev

is it possible to work with multiple 9-patch images at the same time?

From Dev

Laravel PHP: multiple project run at the same time

From Dev

Multiple PHP scripts run at same time in Cron

From Dev

Multiple PHP scripts run at same time in Cron

From Dev

Load images at the same time

From Dev

Uploading multiple images from Share Extension

From Dev

Uploading multiple images with other parameters in Swift

From Dev

Django REST: Uploading and serializing multiple images

From Dev

Uploading multiple images to mysql database on Apache server

From Dev

multiple images uploading and converting them to thumbnails in cakephp

Related Related

  1. 1

    Multiple images rename & uploading in PHP

  2. 2

    Multiple images rename & uploading in PHP

  3. 3

    PHP: Uploading multiple images to imgur at once

  4. 4

    Uploading multiple images in webservice using PHP

  5. 5

    Mysql updating multiple textboxes and uploading multiple files at the same time

  6. 6

    Mysql updating multiple textboxes and uploading multiple files at the same time

  7. 7

    Uploading multiple images with volley?

  8. 8

    Uploading multiple images in codeigniter?

  9. 9

    Uploading multiple images with paperclip?

  10. 10

    Uploading multiple images with Django

  11. 11

    Uploading multiple images in codeigniter?

  12. 12

    Django multiple images not uploading

  13. 13

    Uploading multiple images one to each row PHP and Mysqli db

  14. 14

    Uploading Multiple Images using CarrierWave

  15. 15

    All images not uploading in multiple images upload

  16. 16

    Wont uploading bigger images PHP

  17. 17

    PHP: Images file is not uploading on the server?

  18. 18

    PHP foreach($images as $image) not uploading images

  19. 19

    Uploading multiple files in the same request

  20. 20

    is it possible to work with multiple 9-patch images at the same time?

  21. 21

    Laravel PHP: multiple project run at the same time

  22. 22

    Multiple PHP scripts run at same time in Cron

  23. 23

    Multiple PHP scripts run at same time in Cron

  24. 24

    Load images at the same time

  25. 25

    Uploading multiple images from Share Extension

  26. 26

    Uploading multiple images with other parameters in Swift

  27. 27

    Django REST: Uploading and serializing multiple images

  28. 28

    Uploading multiple images to mysql database on Apache server

  29. 29

    multiple images uploading and converting them to thumbnails in cakephp

HotTag

Archive