file_put_contents

Supported Versions: PHP 5, PHP 7, PHP 8
Write data to a file
<?php file_put_contents(    string $filename,    mixed $data,    int $flags = 0,    ?resource $context = null): int|false
<?php         );

        error_clear_last();



        if (($size = @file_put_contents($prefixedLocation, $contents, $this->writeFlags)) === false) {

            throw UnableToWriteFile::atLocation($path, error_get_last()['message'] ?? '');

        }
<?php     public function deleting_a_file(): void

    {

        $adapter = new LocalFilesystemAdapter(static::ROOT);

        file_put_contents(static::ROOT . '/file.txt', 'contents');

        $adapter->delete('/file.txt');

        $this->assertFileDoesNotExist(static::ROOT . '/file.txt');

    }
<?php     public function checking_if_a_file_exists(): void

    {

        $adapter = new LocalFilesystemAdapter(static::ROOT);

        file_put_contents(static::ROOT . '/file.txt', 'contents');



        $this->assertTrue($adapter->fileExists('/file.txt'));

    }
<?php     public function listing_directory_contents_with_link_skipping(): void

    {

        $adapter = new LocalFilesystemAdapter(static::ROOT, null, LOCK_EX, LocalFilesystemAdapter::SKIP_LINKS);

        file_put_contents(static::ROOT . '/file.txt', 'content');

        symlink(static::ROOT . '/file.txt', static::ROOT . '/link.txt');
<?php     {

        $this->expectException(SymbolicLinkEncountered::class);

        $adapter = new LocalFilesystemAdapter(static::ROOT, null, LOCK_EX, LocalFilesystemAdapter::DISALLOW_LINKS);

        file_put_contents(static::ROOT . '/file.txt', 'content');

        symlink(static::ROOT . '/file.txt', static::ROOT . '/link.txt');
<?php         $adapter = new LocalFilesystemAdapter(static::ROOT);

        mkdir(static::ROOT . '/directory/subdir/', 0744, true);

        $this->assertDirectoryExists(static::ROOT . '/directory/subdir/');

        file_put_contents(static::ROOT . '/directory/subdir/file.txt', 'content');

        symlink(static::ROOT . '/directory/subdir/file.txt', static::ROOT . '/directory/subdir/link.txt');

        $adapter->deleteDirectory('directory/subdir');

        $this->assertDirectoryDoesNotExist(static::ROOT . '/directory/subdir/');