What are some common pitfalls to avoid when using PHP to manage and display files from directories on a website?

One common pitfall to avoid when using PHP to manage and display files from directories on a website is not properly sanitizing user input, which can lead to security vulnerabilities such as directory traversal attacks. To prevent this, always validate and sanitize user input before using it to access files or directories.

// Sanitize user input to prevent directory traversal attacks
$directory = 'path/to/directory/' . basename($_GET['directory']);