Nette\Http\FileUpload::getContentType PHP Method

getContentType() public method

Returns the MIME content type of an uploaded file.
public getContentType ( ) : string | null
return string | null
    public function getContentType()
    {
        if ($this->isOk() && $this->type === NULL) {
            $this->type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $this->tmpName);
        }
        return $this->type;
    }

Usage Example

Esempio n. 1
0
 public function create(Product $product, FileUpload $fileUpload)
 {
     switch ($fileUpload->getContentType()) {
         case 'image/jpeg':
             $suffix = 'jpg';
             break;
         case 'image/png':
             $suffix = 'png';
             break;
         case 'image/gif':
             $suffix = 'gif';
             break;
         default:
             throw new EntityInvalidArgumentException(sprintf('File is of an unknown type %s.', $fileUpload->getContentType()));
     }
     $baseName = sprintf('%s-%%s.%s', Strings::webalize($product->getName()), $suffix);
     do {
         $fileName = sprintf($baseName, Random::generate(5, '0-9a-zA-Z'));
         $path = sprintf('%s/%s', $this->imagesDir, $fileName);
     } while (file_exists($path));
     $fileUpload->move($path);
     $image = new ProductImage($product, $fileName);
     $this->createEntity($image);
     $product->addImage($image);
     return $image;
 }
All Usage Examples Of Nette\Http\FileUpload::getContentType