Toolbox::getMime PHP Méthode

getMime() static public méthode

Retrieve the mime type of a file
static public getMime ( $file, $type = false ) : string
$file string path of the file
$type string check if $file is the correct type (false by default)
Résultat string (if $type not given) else boolean
    static function getMime($file, $type = false)
    {
        static $finfo = NULL;
        if (is_null($finfo)) {
            $finfo = new finfo(FILEINFO_MIME_TYPE);
        }
        $mime = $finfo->file($file);
        if ($type) {
            $parts = explode('/', $mime, 2);
            return $parts[0] == $type;
        }
        return $mime;
    }

Usage Example

Exemple #1
0
 /**
  * Convert tag to image
  *
  * @since version 0.85
  *
  * @param $content_text         text content of input
  * @param $force_update         force update of content in item (false by default
  * @param $doc_data       array of filenames and tags
  *
  * @return nothing
  **/
 function convertTagToImage($content_text, $force_update = false, $doc_data = array())
 {
     global $CFG_GLPI;
     $matches = array();
     // If no doc data available we match all tags in content
     if (!count($doc_data)) {
         $doc = new Document();
         preg_match_all('/' . Document::getImageTag('(([a-z0-9]+|[\\.\\-]?)+)') . '/', $content_text, $matches, PREG_PATTERN_ORDER);
         if (isset($matches[1]) && count($matches[1])) {
             $doc_data = $doc->find("`tag` IN('" . implode("','", array_unique($matches[1])) . "')");
         }
     }
     if (count($doc_data)) {
         foreach ($doc_data as $id => $image) {
             // Add only image files : try to detect mime type
             $ok = false;
             $mime = '';
             if (isset($image['filepath'])) {
                 $fullpath = GLPI_DOC_DIR . "/" . $image['filepath'];
                 $mime = Toolbox::getMime($fullpath);
                 $ok = Toolbox::getMime($fullpath, 'image');
             }
             if (isset($image['tag'])) {
                 if ($ok || empty($mime)) {
                     // Replace tags by image in textarea
                     $img = "<img alt='" . $image['tag'] . "' src='" . $CFG_GLPI['root_doc'] . "/front/document.send.php?docid=" . $id . "&tickets_id=" . $this->fields['id'] . "'/>";
                     // Replace tag by the image
                     $content_text = preg_replace('/' . Document::getImageTag($image['tag']) . '/', Html::entities_deep($img), $content_text);
                     // Replace <br> TinyMce bug
                     $content_text = str_replace(array('&gt;rn&lt;', '&gt;\\r\\n&lt;', '&gt;\\r&lt;', '&gt;\\n&lt;'), '&gt;&lt;', $content_text);
                     // If the tag is from another ticket : link document to ticket
                     // TODO : comment maybe not used
                     // if($image['tickets_id'] != $this->fields['id']){
                     //    $docitem = new Document_Item();
                     //    $docitem->add(array('documents_id'  => $image['id'],
                     //                        '_do_notif'     => false,
                     //                        '_disablenotif' => true,
                     //                        'itemtype'      => $this->getType(),
                     //                        'items_id'      => $this->fields['id']));
                     // }
                 } else {
                     // Remove tag
                     $content_text = preg_replace('/' . Document::getImageTag($image['tag']) . '/', '', $content_text);
                 }
             }
         }
     }
     if ($force_update) {
         $this->fields['content'] = $content_text;
         $this->updateInDB(array('content'));
     }
     return $content_text;
 }
All Usage Examples Of Toolbox::getMime