Unable to Upload Files after Server Migration – Possible Permissions Issue

I’ve been encountering an issue for the last three days while working on a project that involves file and text uploads to my server. Every time a user uploads something, a new folder is created on the server in the format YearMonthDay_HourMinuteSecond.

Previously, on my old server running Ubuntu 22.04 with Apache, the code worked fine. However, after changing servers, I’m facing an issue where I can’t upload files, but the content gets uploaded. I suspect the problem might be related to folder and file permissions.

Here are the permissions for the directories and files:

Apache folder permission:
drwxr-xr-x 13 root root 4096 html/

English folder permission:
drwxr-xr-x 3 root root 4096 en/

Permissions for the upload page and upload script:
-rw-r–r– 1 root root 15498 upload.html
-rw-r–r– 1 root root 2935 upload.php

Upload directory permissions:
drwxrwxrwx 4 www-data www-data 4096 test_upload/

I’m using the latest Apache2 and PHP versions, and I’m running Ubuntu 22.04.

I believe the issue might be related to permissions on the server. Any help or suggestions on resolving this problem would be highly appreciated. Thank you in advance!

upload.php code :

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $title = $_POST["title"];
    $story = $_POST["story"];
    $username = isset($_POST["username"]) ? $_POST["username"] : "Anonymous"; 

    // Get the current date and time in the desired format (YearMonthDay_HourMinuteSecond)
    $currentDateTime = date("Ymd_His");

    // Directory to store files and content
    $uploadDir = "/var/www/html/test_upload/" . $currentDateTime . "/";

    if (!file_exists($uploadDir)) {
        mkdir($uploadDir, 0777, true);
    }

    // Check if files were uploaded
    if (!empty($_FILES["file"]["name"][0])) {
        foreach ($_FILES["file"]["name"] as $key => $fileName) {
            $uploadFile = $uploadDir . basename($fileName);

            // Move uploaded file with proper file type validation
            $fileTmp = $_FILES["file"]["tmp_name"][$key];
            $fileType = mime_content_type($fileTmp);

            // Add more file type validations if needed
            if ($fileType === 'video/mp4' || $fileType === 'video/mpeg') {
                if (move_uploaded_file($fileTmp, $uploadFile)) {
                    // File uploaded successfully
                } else {
                    // File upload failed
                    echo "File upload failed. Debugging information:<br>";
                    echo "Upload file: $uploadFile<br>";
                    echo "Temporary file: $fileTmp<br>";
                    echo "File type: $fileType<br>";
                    echo "Check directory permissions and server configuration.";
                    exit();
                }
            } else {
                // Invalid file type
                echo "Invalid file type. Only MP4 and MPEG videos are allowed.";
                exit();
            }
        }
    }

    // Save the content and user information to a text file in the directory (without geolocation and user agent)
    $content = "Title: $titlenStory: $storynUsername: $username";
    file_put_contents($uploadDir . "content.txt", $content);

    // Redirect to success page
    header("Location: success.html");
    exit();
} else {
    // Redirect to error page
    header("Location: error.html");
    exit();
}
?>

any help with the problem pleas !