What are some best practices for structuring PHP code to handle file uploads and numbering efficiently?

When handling file uploads and numbering efficiently in PHP, it is important to ensure that uploaded files are properly named and stored in a structured manner. One best practice is to append a unique identifier to the file name to prevent overwriting existing files. Additionally, organizing files in separate directories based on categories or dates can help with efficient file management.

// Generate a unique identifier for the uploaded file
$unique_id = uniqid();

// Get the file extension
$file_extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

// Construct the new file name with the unique identifier
$new_file_name = $unique_id . '.' . $file_extension;

// Specify the directory where the file will be stored
$upload_directory = 'uploads/';

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

// Move the uploaded file to the specified directory with the new file name
move_uploaded_file($_FILES['file']['tmp_name'], $upload_directory . $new_file_name);

// Display a success message
echo 'File uploaded successfully.';