How can the issue of creating multiple files under the same name be resolved in PHP?

Issue: To prevent creating multiple files under the same name in PHP, you can check if the file already exists before creating a new one. If the file exists, you can either generate a unique name or handle the situation in a way that suits your application logic.

// Check if the file already exists before creating a new one
$filename = "example.txt";

if (file_exists($filename)) {
    // Handle the situation where the file already exists
    // For example, generate a unique filename or update the existing file
} else {
    // Create the new file
    $file = fopen($filename, "w");
    fwrite($file, "Hello, World!");
    fclose($file);
    echo "File created successfully.";
}