What are some best practices for handling file uploads in PHP when dealing with multiple servers?

When dealing with file uploads in PHP on multiple servers, it is important to ensure that the files are properly synchronized across all servers to avoid inconsistencies. One way to achieve this is by using a central storage system, such as a shared network drive or a cloud storage service, where all uploaded files are stored and accessed by all servers.

// Example code for handling file uploads on multiple servers using a central storage system

// Set the upload directory to the central storage system
$uploadDir = '/central_storage/uploads/';

// Check if the upload directory exists, if not create it
if (!file_exists($uploadDir)) {
    mkdir($uploadDir, 0777, true);
}

// Move the uploaded file to the central storage system
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadDir . $_FILES['file']['name'])) {
    echo 'File uploaded successfully.';
} else {
    echo 'Failed to upload file.';
}