What are the best practices for handling file paths in PHP scripts executed by cron jobs?

When writing PHP scripts executed by cron jobs, it's important to handle file paths correctly to avoid any issues with file operations. One best practice is to use absolute paths for all file references to ensure the script can locate the files regardless of the current working directory. Additionally, it's helpful to define a constant or variable for the base directory of the script and use it to construct all file paths.

<?php
// Define base directory for the script
define('BASE_DIR', '/var/www/html/myproject');

// Construct absolute file paths using the base directory
$file1 = BASE_DIR . '/data/file1.txt';
$file2 = BASE_DIR . '/logs/log.txt';

// Perform file operations using the constructed paths
$data = file_get_contents($file1);
file_put_contents($file2, 'Log entry: ' . date('Y-m-d H:i:s'));
?>