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     public function it_can_backup_existing_lock_file()

    {

        file_put_contents($this->lockPath, $content = 'test lock file content');



        $this->assertFileExists($this->lockPath);

        $this->assertFileNotExists($this->backupLockPath);
<?php             mkdir($dir);

        }



        file_put_contents($this->customLockPath, $content = 'custom lock file content');



        $this->assertFileExists($this->customLockPath);

        $this->assertFileNotExists($this->customBackupLockPath);
<?php             'version' => $version,

        ], $extra);



        file_put_contents(

            static::preparePath($path ?? static::DEFAULT_TEST_PACKAGE_LOCATION.'/composer.json'),

            json_encode($content, JSON_UNESCAPED_SLASHES)

        );

    }
<?php             $nonFavouritePackagesKey => [],

        ];



        file_put_contents(

            static::preparePath($path ?? static::DEFAULT_TEST_PACKAGE_LOCATION.'/composer.lock'),

            json_encode($content, JSON_UNESCAPED_SLASHES)

        );

    }
<?php             'packages-dev' => [],

        ];



        file_put_contents(

            static::preparePath($path ?? static::DEFAULT_TEST_PACKAGE_LOCATION.'/composer.lock'),

            json_encode($content, JSON_UNESCAPED_SLASHES)

        );

    }
<?php     public function it_gets_files_from_outside_of_the_root_and_outputs_absolute_paths()

    {

        mkdir($this->outsideRoot.'/sub', 0755, true);

        file_put_contents($this->outsideRoot.'/sub/one.txt', '');

        file_put_contents($this->outsideRoot.'/sub/two.txt', '');



        $dir = Path::tidy($this->outsideRoot);
<?php     {

        mkdir($this->outsideRoot.'/sub', 0755, true);

        file_put_contents($this->outsideRoot.'/sub/one.txt', '');

        file_put_contents($this->outsideRoot.'/sub/two.txt', '');



        $dir = Path::tidy($this->outsideRoot);
<?php     public function it_can_explicitly_request_absolute_paths()

    {

        mkdir($this->tempDir.'/sub/sub', 0755, true);

        file_put_contents($this->tempDir.'/one.txt', '');

        file_put_contents($this->tempDir.'/sub/two.txt', '');

        file_put_contents($this->tempDir.'/sub/three.txt', '');

        file_put_contents($this->tempDir.'/sub/sub/four.txt', '');
<?php     {

        mkdir($this->tempDir.'/sub/sub', 0755, true);

        file_put_contents($this->tempDir.'/one.txt', '');

        file_put_contents($this->tempDir.'/sub/two.txt', '');

        file_put_contents($this->tempDir.'/sub/three.txt', '');

        file_put_contents($this->tempDir.'/sub/sub/four.txt', '');
<?php         mkdir($this->tempDir.'/sub/sub', 0755, true);

        file_put_contents($this->tempDir.'/one.txt', '');

        file_put_contents($this->tempDir.'/sub/two.txt', '');

        file_put_contents($this->tempDir.'/sub/three.txt', '');

        file_put_contents($this->tempDir.'/sub/sub/four.txt', '');



        $return = $this->adapter->withAbsolutePaths();
<?php         file_put_contents($this->tempDir.'/one.txt', '');

        file_put_contents($this->tempDir.'/sub/two.txt', '');

        file_put_contents($this->tempDir.'/sub/three.txt', '');

        file_put_contents($this->tempDir.'/sub/sub/four.txt', '');



        $return = $this->adapter->withAbsolutePaths();

        $this->assertEquals($this->adapter, $return);
<?php     public function gets_file_contents()

    {

        file_put_contents($this->tempDir.'/filename.txt', 'Hello World');

        $this->assertEquals('Hello World', $this->adapter->get('filename.txt'));

    }
<?php     public function checks_if_file_exists()

    {

        file_put_contents($this->tempDir.'/filename.txt', 'Hello World');

        $this->assertTrue($this->adapter->exists('filename.txt'));

        $this->assertFalse($this->adapter->exists('another.txt'));

    }
<?php     public function deletes_files()

    {

        file_put_contents($this->tempDir.'/filename.txt', 'Hello World');

        $this->adapter->delete('filename.txt');

        $this->assertFileNotExists($this->tempDir.'/filename.txt');

    }
<?php     public function copies_files()

    {

        file_put_contents($this->tempDir.'/src.txt', 'Hello World');

        $this->assertTrue($this->adapter->copy('src.txt', 'dest.txt'));

        $this->assertFileExists($this->tempDir.'/dest.txt');

        $this->assertFileExists($this->tempDir.'/src.txt');