What are best practices for specifying file paths in PHP scripts when writing data to a different directory on the server?

When writing data to a different directory on the server in PHP scripts, it is important to specify the file path correctly to ensure that the script can access and write to the desired location. One best practice is to use the `$_SERVER['DOCUMENT_ROOT']` variable to get the root directory of the server and then concatenate it with the relative path to the desired directory. This ensures portability and security in your script.

// Specify the file path to the desired directory using $_SERVER['DOCUMENT_ROOT']
$directory = $_SERVER['DOCUMENT_ROOT'] . '/path/to/directory/';

// Write data to a file in the specified directory
$file = $directory . 'example.txt';
$data = "Hello, world!";
file_put_contents($file, $data);