Phly\Http\UploadedFile::moveTo PHP Method

moveTo() public method

See also: http://php.net/is_uploaded_file
See also: http://php.net/move_uploaded_file
public moveTo ( string $targetPath )
$targetPath string Path to which to move the uploaded file.
    public function moveTo($targetPath)
    {
        if (!is_string($targetPath)) {
            throw new InvalidArgumentException('Invalid path provided for move operation; must be a string');
        }
        if (empty($targetPath)) {
            throw new InvalidArgumentException('Invalid path provided for move operation; must be a non-empty string');
        }
        if ($this->moved) {
            throw new RuntimeException('Cannot move file; already moved!');
        }
        $sapi = PHP_SAPI;
        switch (true) {
            case empty($sapi) || 0 === strpos($sapi, 'cli') || !$this->file:
                // Non-SAPI environment, or no filename present
                $this->writeFile($targetPath);
                break;
            default:
                // SAPI environment, with file present
                if (false === move_uploaded_file($this->file, $targetPath)) {
                    throw new RuntimeException('Error occurred while moving uploaded file');
                }
                break;
        }
        $this->moved = true;
    }

Usage Example

Beispiel #1
0
 public function testCannotRetrieveStreamAfterMove()
 {
     $stream = new Stream('php://temp', 'wb+');
     $stream->write('Foo bar!');
     $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
     $this->tmpFile = $to = tempnam(sys_get_temp_dir(), 'phly');
     $upload->moveTo($to);
     $this->assertTrue(file_exists($to));
     $this->setExpectedException('RuntimeException', 'moved');
     $upload->getStream();
 }