/** * Test that getExtFromType() returns a list of ext from the mime type. */ public function testGetExtFromType() { $this->assertEquals(array('jpe', 'jpeg', 'jpg'), MimeType::getExtFromType('image/jpeg')); $this->assertEquals('jpg', MimeType::getExtFromType('image/jpeg', true)); $this->assertEquals(array('png'), MimeType::getExtFromType('image/png')); $this->assertEquals(array(), MimeType::getExtFromType('foo/bar')); $this->assertEquals('txt', MimeType::getExtFromType('text/plain', true)); }
/** * Rename the file within the current directory. * * @param string $name * @param string $append * @param string $prepend * @return bool */ public function rename($name = '', $append = '', $prepend = '') { if (is_callable($name)) { $name = call_user_func_array($name, array($this->name(), $this)); } else { $name = $name ?: $this->name(); } // Add boundaries $name = (string) $prepend . $name . (string) $append; // Remove unwanted characters $name = preg_replace('/[^_-\\p{Ll}\\p{Lm}\\p{Lo}\\p{Lt}\\p{Lu}\\p{Nd}]/imu', '-', $name); // Rename file $ext = $this->ext() ?: MimeType::getExtFromType($this->type(), true); $targetPath = $this->dir() . $name . '.' . $ext; if (rename($this->path(), $targetPath)) { $this->reset($targetPath); return true; } return false; }