What are some best practices for handling file paths and directories in PHP file upload scripts?

When handling file paths and directories in PHP file upload scripts, it is important to sanitize user input to prevent directory traversal attacks. One best practice is to use the `realpath()` function to get the absolute path of the directory where files will be saved. Additionally, ensure that the directory has the correct permissions set to allow file uploads.

// Get the absolute path of the upload directory
$uploadDir = realpath('uploads');

// Check if the directory exists and is writable
if ($uploadDir && is_writable($uploadDir)) {
    // Handle file upload logic here
} else {
    echo "Upload directory is not writable.";
}