When I try to upload more than 10 images via...

index.php

<?php include("file-upload.php"); ?>
<div>
<form action="" method="post" enctype="multipart/form-data" class="mb-3">
      <div class="user-image mb-3 text-center">
        <div class="imgGallery"> 
          <!-- Image preview -->
        </div>
      </div>
      <div class="custom-file">
        <input type="file" name="fileUpload[]" class="custom-file-input" id="chooseFile" multiple>
        <label class="custom-file-label" for="chooseFile">Select image files</label>
      </div>
      <button type="submit" name="submit" class="btn btn-primary btn-block mt-4">
        Upload Images
      </button>
    </form>
    <!-- Display response messages -->
    <?php if(!empty($response)) {?>
        <div class="alert <?php echo $response["status"]; ?>">
           <?php echo $response["message"]; ?>
        </div>
    <?php }?>
  </div>
  <!-- jQuery -->
  <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
  <script>
    $(function() {
    // Multiple images preview with JavaScript
    var multiImgPreview = function(input, imgPreviewPlaceholder) {

        if (input.files) {
            var filesAmount = input.files.length;

            for (i = 0; i < filesAmount; i++) {
                var reader = new FileReader();

                reader.onload = function(event) {
                    $($.parseHTML('<img>')).attr('src', event.target.result).appendTo(imgPreviewPlaceholder);
                }

                reader.readAsDataURL(input.files[i]);
            }
        }

    };

      $('#chooseFile').on('change', function() {
        multiImgPreview(this, 'div.imgGallery');
      });
    });    
  </script>

file-upload.php

// Database
include 'config/database.php'; 

if(isset($_POST['submit'])){
    
    $uploadsDir = "uploads/";
    $allowedFileType = array('jpg','png','jpeg');
    
    // Velidate if files exist
    if (!empty(array_filter($_FILES['fileUpload']['name']))) {
        
        // Loop through file items
        foreach($_FILES['fileUpload']['name'] as $id=>$val){
            // Get files upload path
            $uploadDate4file      = date('dmYHis');
            $fileName        = $_FILES['fileUpload']['name'][$id];
            $tempLocation    = $_FILES['fileUpload']['tmp_name'][$id];
            $targetFilePath  = $uploadsDir . $uploadDate4file . $fileName;
            $fileType        = strtolower(pathinfo($targetFilePath, PATHINFO_EXTENSION));
            $uploadDate      = date('Y-m-d H:i:s');
            $uploadOk = 1;

            if(in_array($fileType, $allowedFileType)){
                    if(move_uploaded_file($tempLocation, $targetFilePath)){
                        $sqlVal = "('".$fileName."', '".$uploadDate."')";
                    } else {
                        $response = array(
                            "status" => "alert-danger",
                            "message" => "File could not be uploaded."
                        );
                    }
                
            } else {
                $response = array(
                    "status" => "alert-danger",
                    "message" => "Only .jpg, .jpeg and .png file formats allowed."
                );
            }
            // Add into MySQL database
            if(!empty($sqlVal)) {
                $insert = $conn->query("INSERT INTO productimages (images, date_time) VALUES $sqlVal");
                if($insert) {
                    $response = array(
                        "status" => "alert-success",
                        "message" => "Images successfully uploaded."
                    );
                        ///Prints $rmfirstclassuk as array 
                        
echo "Images uploaded: ".$_FILES['fileUpload']['name'].", ";
echo "<pre>";
print_r ($_FILES['fileUpload']['name']);
echo "</pre>";
                    
                } else {
                    $response = array(
                        "status" => "alert-danger",
                        "message" => "Images couldn't be uploaded due to database error."
                    );
                }
            }
        }

    } else {
        // Error
        $response = array(
            "status" => "alert-danger",
            "message" => "Please select image files to upload."
        );
    }
} 

database.php

$hostname = "HIDDEN";
$username = "HIDDEN";
$password = "HIDDEN";
try {
    $conn = new PDO("mysql:host=$hostname;dbname=mydbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    //echo "Database connected successfully";
} catch(PDOException $e) {
    echo "Database connection failed: " . $e->getMessage();
}

...only the first 10 ever actually upload.

On reading other answers on here I have changed max_file_uploads to 50 on the PHP Configuration page and also tried ini_set('max_file_uploads', 50); on the page itself but try as I might I can't get it to upload more than 10 files.

Note: despite me trying to set the limit to 50 I only actually want to upload 12 at a time so I'm falling just 2 short!

So then I contacted my host with the issue who first tried upping my max_file_uploads to 100 and asked me to try again but when that didn't work have now said...

I've checked this further. It doesn't look to be an issue with the server block I've both our mod security rule and server restrictions but all looks to be allowing it. It does seem to be that the code is preventing this from working.

but as much as I look over the code I can't see anything that would be preventing more than 10 files uploading, any ideas please?

0

There are 0 best solutions below