$_FILES empty after submit

ghz 7months ago ⋅ 76 views

I'm doing a little project where the user can upload multiple images from a simple form. When I submit the form I go in a service page where I must save the images, but this not working. So, I tried to print $_FILES, and I saw that it's empty. Where is the mistake?

This is the HTML code with Bootstrap:

<form action="salvataggio-foto.php" method="post" enctype="multipart/form-data">
  <div class="mb-3">
     <input class="form-control" type="file" id="images" name="images[]" multiple>
  </div>
  <button type="submit" class="btn btn-primary" name="invia">Carica</button>
</form>

In the service page, I have a simple print_r($_FILES).

Someone can helps me? Thank you so much :)

Answers

Your HTML form looks correct, and you're using the correct enctype attribute for file uploads. If $_FILES is empty when you submit the form, it might be due to a few common reasons:

  1. PHP Configuration: Ensure that your PHP server is configured to accept file uploads. Check the upload_max_filesize and post_max_size directives in your php.ini file.

  2. Form Element Naming: Make sure that the name attribute of your file input element is correctly set to "images[]" to allow multiple file uploads.

  3. File Size Limit: If your uploaded files are large, they may exceed the server's file size limit. Check and adjust the upload_max_filesize directive in your php.ini file if necessary.

  4. File Upload Limit: Some servers impose limits on the number of files that can be uploaded in a single request. Ensure that your server's configuration allows for multiple file uploads.

  5. File Permissions: Ensure that the directory where you're trying to save the uploaded files has the correct permissions set to allow PHP to write files to it.

  6. Form Submission: Verify that you're submitting the form correctly and that it's being submitted to the correct URL (salvataggio-foto.php in your case).

If you've checked these common issues and are still experiencing problems, you may need to look into server logs or PHP error messages to determine the cause of the issue. Additionally, you can try adding some error handling to your PHP script to catch any potential errors during file upload:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!empty($_FILES['images']['name'])) {
        // Loop through each uploaded file
        foreach ($_FILES['images']['tmp_name'] as $key => $tmp_name) {
            $file_name = $_FILES['images']['name'][$key];
            $file_tmp = $_FILES['images']['tmp_name'][$key];
            $file_size = $_FILES['images']['size'][$key];
            $file_type = $_FILES['images']['type'][$key];
            $file_error = $_FILES['images']['error'][$key];

            // Check for file errors
            if ($file_error !== UPLOAD_ERR_OK) {
                echo "Error uploading file: $file_name (Error code: $file_error)";
                continue; // Skip this file
            }

            // Move uploaded file to destination directory
            move_uploaded_file($file_tmp, "upload/$file_name");
            echo "File uploaded successfully: $file_name";
        }
    } else {
        echo "No files uploaded";
    }
} else {
    echo "Invalid request method";
}
?>

This script will handle multiple file uploads, check for errors during upload, and move the uploaded files to a directory named "upload" within the same directory as the script. Make sure to create the "upload" directory and give it appropriate permissions to allow PHP to write files to it.