What are some potential pitfalls when using PHP to extract data from a URL and write it to a file?

One potential pitfall when using PHP to extract data from a URL and write it to a file is not properly handling errors or exceptions that may occur during the process. To mitigate this, you should implement error handling to catch any potential issues that may arise.

<?php
$url = "https://www.example.com/data.txt";
$file = "output.txt";

$data = file_get_contents($url);

if($data === false){
    echo "Error fetching data from URL";
    exit;
}

if(file_put_contents($file, $data) === false){
    echo "Error writing data to file";
    exit;
}

echo "Data successfully extracted from URL and written to file";
?>