luya\helpers\FileHelper::ensureExtension PHP Method

ensureExtension() public static method

Append a file extension to a path/file if there is no or an empty extension provided, this helper methods is used to make sure the right extension existing on files.
public static ensureExtension ( string $file, string $extension ) : the
$file string The file where extension should be append if not existing
$extension string
return the ensured file/path with extension
    public static function ensureExtension($file, $extension)
    {
        $info = pathinfo($file);
        if (!isset($info['extension']) || empty($info['extension'])) {
            $file = rtrim($file, '.') . '.' . $extension;
        }
        return $file;
    }

Usage Example

Beispiel #1
0
 public function testEnsureExtension()
 {
     $this->assertSame('path/to/image.png', FileHelper::ensureExtension('path/to/image.png', 'gif'));
     $this->assertSame('path/to/image.gif', FileHelper::ensureExtension('path/to/image', 'gif'));
     // twig example as used in element component
     $this->assertSame('file.twig', FileHelper::ensureExtension('file', 'twig'));
     $this->assertSame('path/to/file.twig', FileHelper::ensureExtension('path/to/file', 'twig'));
     $this->assertSame('path/to/file.twig', FileHelper::ensureExtension('path/to/file.', 'twig'));
     $this->assertSame('path/to/file.twig', FileHelper::ensureExtension('path/to/file.twig', 'twig'));
 }
All Usage Examples Of luya\helpers\FileHelper::ensureExtension