Symfony\Component\HttpFoundation\File\File::getExtension PHP Method

getExtension() public method

\SplFileInfo::getExtension() is not available before PHP 5.3.6
public getExtension ( ) : string
return string The extension
    public function getExtension()
    {
        return pathinfo($this->getBasename(), PATHINFO_EXTENSION);
    }

Usage Example

示例#1
0
 /**
  * {@inheritdoc}
  */
 public function buildView(FormView $view, FormInterface $form, array $options)
 {
     parent::buildView($view, $form, $options);
     $data = $form->getData();
     if (is_string($data)) {
         $data = ['filename' => basename($data), 'url' => $data];
     }
     if (is_array($data)) {
         if (!isset($view->var['url']) && isset($data['url'])) {
             $view->vars['url'] = $data['url'];
         }
         if (!isset($view->var['filename']) && isset($data['filename'])) {
             $view->vars['filename'] = $data['filename'];
         }
     }
     // autodetect type of upload
     $uploadtype = $options['uploadtype'];
     if (isset($view->vars['filename'])) {
         $file = new File($view->vars['filename'], false);
         $view->vars['filetype'] = $file->getExtension();
         switch ($file->getExtension()) {
             case 'png':
             case 'jpg':
             case 'jpeg':
             case 'gif':
                 $uploadtype = 'image';
                 break;
             case 'mp4':
                 $uploadtype = 'video';
                 break;
             case 'mp3':
             case 'm4v':
                 $uploadtype = 'audio';
                 break;
             default:
                 break;
         }
     }
     $view->vars['uploadtype'] = $uploadtype;
     $view->vars['placeholdWidth'] = $options['placeholdWidth'];
     $view->vars['placeholdHeight'] = $options['placeholdHeight'];
     $view->vars['placeholdText'] = $options['placeholdText'];
     $view->vars['ratio'] = $options['ratio'];
     $view->vars['remove'] = $options['remove'];
     $pattern = $options['pattern'];
     if ($pattern != null) {
         if (!$this->container->hasParameter($pattern)) {
             throw new \Exception('You must define an existing pattern');
         }
         $patternSize = $this->container->getParameter($pattern);
         $view->vars['label'] .= ' (Max width: ' . $patternSize['width'] . ', Max height: ' . $patternSize['height'] . ')';
     }
 }
All Usage Examples Of Symfony\Component\HttpFoundation\File\File::getExtension