What are the potential pitfalls of storing uploaded images in the root directory in PHP?

Storing uploaded images in the root directory can pose security risks as it allows direct access to sensitive files. To mitigate this, it is recommended to store uploaded images in a separate directory outside of the root directory. This ensures that the files are not directly accessible via a URL.

// Define the directory to store uploaded images
$uploadDirectory = 'uploads/';

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

// Move the uploaded file to the specified directory
move_uploaded_file($_FILES['image']['tmp_name'], $uploadDirectory . $_FILES['image']['name']);