$file = new SplFileObject('example.txt', 'w'); $file->fwrite('This is a new line of text');
$iterator = new FilesystemIterator('/path/to/directory'); foreach ($iterator as $file) { if ($file->isFile()) { $content = file_get_contents($file->getPathname()); $newContent = str_replace('old string', 'new string', $content); file_put_contents($file->getPathname(), $newContent); } }
use Symfony\Component\Filesystem\Filesystem; $filesystem = new Filesystem(); $filesystem->appendToFile('example.txt', 'This is another line of text');In this example, we create a new instance of the Filesystem class from the Symfony Filesystem Component. We then use the appendToFile() method to add the string "This is another line of text" to the end of the file "example.txt". Overall, there are many different tools available for updating files in PHP, and the best choice will depend on the specific needs and requirements of the application.