Are there any best practices to follow when creating folders with PHP?

When creating folders with PHP, it is important to follow best practices to ensure security and maintainability. One common best practice is to sanitize user input to prevent directory traversal attacks. Additionally, it is recommended to check if the folder already exists before creating it to avoid overwriting existing directories.

// Sanitize user input for folder name
$folderName = filter_var($_POST['folder_name'], FILTER_SANITIZE_STRING);

// Check if folder already exists
if (!file_exists($folderName)) {
    // Create the folder
    mkdir($folderName);
    echo "Folder created successfully.";
} else {
    echo "Folder already exists.";
}