What are the potential risks of storing files outside of the document root in PHP?

Storing files outside of the document root in PHP can pose security risks as it may expose sensitive information or allow unauthorized access to files. To mitigate this risk, it is recommended to store files within the document root or use proper access control mechanisms to restrict access to files stored outside the document root.

// Example of storing files within the document root
$uploadDir = 'uploads/';
$targetFile = $uploadDir . basename($_FILES['file']['name']);

if (move_uploaded_file($_FILES['file']['tmp_name'], $targetFile)) {
    echo "File uploaded successfully";
} else {
    echo "Error uploading file";
}