Skip to content

Commit

Permalink
File::move now returns new file instance for relocated file. Fixes #1782
Browse files Browse the repository at this point in the history
lonnieezell committed Mar 22, 2019

Verified

This commit was signed with the committer’s verified signature.
lonnieezell Lonnie Ezell
1 parent f5563e6 commit 427f3ad
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion system/Files/File.php
Original file line number Diff line number Diff line change
@@ -174,7 +174,7 @@ public function move(string $targetPath, string $name = null, bool $overwrite =

@chmod($targetPath, 0777 & ~umask());

return true;
return new File($destination);
}

//--------------------------------------------------------------------
20 changes: 20 additions & 0 deletions tests/system/Files/FileWithVfsTest.php
Original file line number Diff line number Diff line change
@@ -5,7 +5,15 @@
class FileWithVfsTest extends \CIUnitTestCase
{

// For VFS stuff
protected $root;
protected $path;
protected $start;

/**
* @var \CodeIgniter\Files\File
*/
protected $file;

protected function setUp()
{
@@ -117,4 +125,16 @@ public function testMoveFailure()
$this->file->move($destination); // try to move our file there
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1782
*/
public function testMoveReturnsNewInstance()
{
$destination = $this->start . 'baker';
$file = $this->file->move($destination);

$this->assertTrue($this->root->hasChild('baker/apple.php'));
$this->assertInstanceOf(File::class, $file);
$this->assertEquals($destination . '/apple.php', $file->getPathname());
}
}
5 changes: 5 additions & 0 deletions user_guide_src/source/libraries/files.rst
Original file line number Diff line number Diff line change
@@ -98,3 +98,8 @@ By default, the original filename was used. You can specify a new filename by pa

$newName = $file->getRandomName();
$file->move(WRITEPATH.'uploads', $newName);

The move() method returns a new File instance that for the relocated file, so you must capture the result if the
resulting location is needed::

$file = $file->move(WRITEPATH.'uploads');

0 comments on commit 427f3ad

Please sign in to comment.