/** * Simple method to move (small) uploaded files in the filesystem * If the file's size is 0, it simply returns; * @param UploadedFile $file the file to be moved * @param string $path the path where to copy the file * @param string $newBasename the file's new basename (name without extension) * @throws \RuntimeException in case of errors * @return null */ public static function writeUploadedFileToPath(UploadedFile $file, $path, $newBasename) { if (!$file->getSize()) { return; } $ext = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION); $filename = $newBasename . ".{$ext}"; $path = $path . $filename; file_put_contents($path, $file->getStream()->getContents()); }
/** * @return UploadedFile */ public function testConstructor() { $attr = ['tmp_name' => self::$filename, 'name' => 'my-avatar.txt', 'size' => 8, 'type' => 'text/plain', 'error' => 0]; $uploadedFile = new UploadedFile($attr['tmp_name'], $attr['name'], $attr['type'], $attr['size'], $attr['error'], false); $this->assertEquals($attr['name'], $uploadedFile->getClientFilename()); $this->assertEquals($attr['type'], $uploadedFile->getClientMediaType()); $this->assertEquals($attr['size'], $uploadedFile->getSize()); $this->assertEquals($attr['error'], $uploadedFile->getError()); return $uploadedFile; }