How can relative paths and file permissions impact the ability to write data to a text file in PHP?

Relative paths and file permissions can impact the ability to write data to a text file in PHP by restricting access to the file or by pointing to the wrong directory. To solve this issue, ensure that the file path is correct and that the file has the appropriate write permissions set.

$file = 'data.txt'; // Relative path to the text file
if (is_writable($file)) {
    $data = "Hello, World!";
    file_put_contents($file, $data);
    echo "Data written to file successfully.";
} else {
    echo "Cannot write to file. Check file permissions.";
}