What are the best practices for setting up directory paths in PHP scripts to avoid errors related to missing directories?

When setting up directory paths in PHP scripts, it is important to use absolute paths instead of relative paths to avoid errors related to missing directories. Absolute paths specify the complete path from the root directory, ensuring that the script can locate the directories correctly regardless of where the script is executed from. Example PHP code snippet:

// Define absolute path to the directory
$directory_path = '/var/www/html/project/';

// Use the absolute path in your script
$file_path = $directory_path . 'file.txt';

// Use the file_path variable in your code
$file_contents = file_get_contents($file_path);