What are some best practices for organizing and securing files in a PHP application, especially when dealing with user-generated content?

When dealing with user-generated content in a PHP application, it is important to properly organize and secure the files to prevent unauthorized access or malicious activities. One best practice is to store user-generated files outside of the web root directory to prevent direct access through URLs. Additionally, it is recommended to use unique filenames, validate file types, and implement proper file permissions to restrict access.

// Example of storing user-generated file outside of web root directory
$uploadDir = '/var/www/uploads/';
$filename = uniqid() . '_' . $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $uploadDir . $filename);