How can you add new content to the beginning of a file in PHP without overwriting existing content?

To add new content to the beginning of a file in PHP without overwriting existing content, you can read the existing content of the file, prepend the new content to it, and then write the combined content back to the file.

$file = 'example.txt';
$newContent = "New content to add\n";

// Read the existing content of the file
$existingContent = file_get_contents($file);

// Prepend the new content to the existing content
$combinedContent = $newContent . $existingContent;

// Write the combined content back to the file
file_put_contents($file, $combinedContent);