Are there any best practices for handling file locations and directories in PHP code?

When working with file locations and directories in PHP code, it is best practice to use absolute paths rather than relative paths to ensure consistency and avoid unexpected behavior. Additionally, it is important to sanitize user input to prevent directory traversal attacks and ensure that the code is secure.

// Example of using absolute path and sanitizing user input
$directory = '/path/to/directory/';
$filename = $_GET['file']; // Assume this is user input

// Sanitize user input
$filename = basename($filename);

// Combine directory and filename to get the absolute path
$filepath = $directory . $filename;

// Now you can safely use $filepath in your file operations