Example #1
0
 public function testFile()
 {
     $fp = tmpfile();
     fwrite($fp, self::CONTENTS);
     $filename = stream_get_meta_data($fp)['uri'];
     $this->assertSame('text/plain', (new Finfo(FILEINFO_MIME_TYPE))->file($filename));
     $this->assertSame('text/plain', (new Finfo())->file($filename, FILEINFO_MIME_TYPE));
     $finfo = new Finfo();
     $finfo->set_flags(FILEINFO_MIME_TYPE);
     $this->assertSame('text/plain', $finfo->file($filename));
 }
Example #2
0
 /**
  * Gets the media type of this file.
  *
  * @link https://en.wikipedia.org/wiki/Media_type
  * @return string|bool Media type as a string on success, false on failure.
  */
 public function getMediaType()
 {
     // create finfo object
     $finfo = new \Finfo(FILEINFO_MIME);
     // get mime type
     $type = $finfo->file($this->getRealPath());
     // return type
     return $type;
 }
Example #3
0
function showImages()
{
    $out = "<h1>Image Gallery</h1>";
    $out .= "<ul id='images'>";
    $folder = "img";
    $filesInFolder = new DirectoryIterator($folder);
    while ($filesInFolder->valid()) {
        $file = $filesInFolder->current();
        $fileName = $file->getFileName();
        $src = "{$folder}/{$fileName}";
        $fileInfo = new Finfo(FILEINFO_MIME_TYPE);
        $mimeType = $fileInfo->file($src);
        if ($mimeType === 'image/jpeg') {
            $out .= "<li><img src='{$src}' /></li>";
        }
        $filesInFolder->next();
    }
    $out .= "</ul>";
    return $out;
}
Example #4
0
function showImages()
{
    $out = "<h1>Image Gallery</h1>";
    $out .= "<ul id='images'";
    $folder = "img";
    $filesInFolder = new DirectoryIterator($folder);
    //Iterate through the directory for images
    //with different file types
    while ($filesInFolder->valid()) {
        $file = $filesInFolder->current();
        $filename = $file->getFilename();
        $src = "{$folder}/{$filename}";
        $fileInfo = new Finfo(FILEINFO_MIME_TYPE);
        $mimeType = $fileInfo->file($src);
        if ($mimeType === 'image/jpeg' || $mimeType === 'image/pjpeg' || $mimeType === 'image/png' || $mimeType === 'image/gif') {
            $out .= "<li><img src='{$src}' /></li>";
        }
        $filesInFolder->next();
    }
    $out .= "</ul>";
    return $out;
}
Example #5
0
 /**
  * Returns an excerpt of a code file around the given line number.
  *
  * @param string $file A file path
  * @param int    $line The selected line number
  *
  * @return string An HTML string
  */
 public function fileExcerpt($file, $line)
 {
     if (is_readable($file)) {
         if (extension_loaded('fileinfo')) {
             $finfo = new \Finfo();
             // Check if the file is an application/octet-stream (eg. Phar file) because highlight_file cannot parse these files
             if ('application/octet-stream' === $finfo->file($file, FILEINFO_MIME_TYPE)) {
                 return;
             }
         }
         // highlight_file could throw warnings
         // see https://bugs.php.net/bug.php?id=25725
         $code = @highlight_file($file, true);
         // remove main code/span tags
         $code = preg_replace('#^<code.*?>\\s*<span.*?>(.*)</span>\\s*</code>#s', '\\1', $code);
         $content = preg_split('#<br />#', $code);
         $lines = array();
         for ($i = max($line - 3, 1), $max = min($line + 3, count($content)); $i <= $max; ++$i) {
             $lines[] = '<li' . ($i == $line ? ' class="selected"' : '') . '><code>' . self::fixCodeMarkup($content[$i - 1]) . '</code></li>';
         }
         return '<ol start="' . max($line - 3, 1) . '">' . implode("\n", $lines) . '</ol>';
     }
 }
Example #6
0
 /**
  * {@inheritDoc}
  */
 public function upload($path, $target)
 {
     $finfo = new \Finfo(FILEINFO_MIME_TYPE);
     return $this->client->putObject(array('Bucket' => $this->bucket, 'Key' => $this->computePath($target), 'SourceFile' => $path, 'ContentType' => $finfo->file($path), 'ACL' => $this->config('acl', 'public-read')));
 }
 /**
  * Get the mime type of a file.
  *
  * @param $path
  *
  * @return string
  */
 public static function getMimetype($path)
 {
     $finfo = new Finfo(FILEINFO_MIME_TYPE);
     return $finfo->file($path);
 }
Example #8
0
 /**
  * Gets mime type of a content
  *
  * @param $content
  * @return string
  */
 public static function getContentMime($content)
 {
     $finfo = new Finfo(FILEINFO_MIME_TYPE);
     return $finfo->buffer($content);
 }
 /**
  * @param $path
  * @return string
  */
 public function mime($path)
 {
     $fileInfo = new \Finfo(FILEINFO_MIME_TYPE);
     return $fileInfo->file($this->storagePath($path));
 }
Example #10
0
<?php

if (isset($uploadMessage) === false) {
    $uploadMessage = "Upload a new image";
}
$imagesAsHTML = "<h1>Images</h1>";
$imagesAsHTML .= "<dl id='images'>";
$folder = "img";
$filesInFolder = new DirectoryIterator($folder);
while ($filesInFolder->valid()) {
    $file = $filesInFolder->current();
    $filename = $file->getFilename();
    $src = "{$folder}/{$filename}";
    $fileInfo = new Finfo(FILEINFO_MIME_TYPE);
    $mimeType = $fileInfo->file($src);
    if ($mimeType === 'image/jpeg') {
        $href = "admin.php?page=images&amp;delete-image={$src}";
        $imagesAsHTML .= "<dt><img src='{$src}' /></dt>\n                  <dd>Source: {$src} <a href='{$href}'>delete</dd>";
    }
    $filesInFolder->next();
}
$imagesAsHTML .= "</dl>";
return "\n  <form method='post' action='admin.php?page=images' enctype='multipart/form-data'>\n    <p>{$uploadMessage}</p>\n    <input type='file' name='image-data' accept='image/jpeg' />\n    <input type='submit' name='new-image' value='upload' />\n  </form>\n  <div>\n    {$imagesAsHTML}\n  </div>\n";
Example #11
0
 /**
  * Get the mimetype of a file
  *
  * @param $path
  * @return array
  */
 public function getMimetype($path)
 {
     $location = $this->applyPathPrefix($path);
     $finfo = new Finfo(FILEINFO_MIME_TYPE);
     return ['mimetype' => $finfo->file($location)];
 }
 public function parse(\SplFileInfo $file, string $filename = null, string $title = null) : Dictionary
 {
     $this->filenames = [];
     if ($file instanceof \SplTempFileObject) {
         $binary = (new \esperecyan\dictionary_php\Parser())->getBinary($file);
     }
     if (!$file instanceof \SplFileObject) {
         $file = $file->openFile();
     } else {
         $file->rewind();
     }
     $finfo = new Finfo(FILEINFO_MIME_TYPE);
     $type = isset($binary) ? $finfo->buffer($binary) : $finfo->file($file->getRealPath());
     switch ($type) {
         case 'application/zip':
             $this->header = true;
             $quizFile = $this->parseArchive($file);
             $binary = file_get_contents($quizFile);
             if (!mb_check_encoding($binary, 'Windows-31J')) {
                 throw new SyntaxException(sprintf(_('%sの辞書の符号化方式 (文字コード) は Shift_JIS でなければなりません。'), 'Inteligenceω'));
             }
             $txt = new \SplTempFileObject();
             $txt->fwrite(mb_convert_encoding($binary, 'UTF-8', 'Windows-31J'));
             $files = new \FilesystemIterator($quizFile->getPath(), \FilesystemIterator::SKIP_DOTS);
             unlink($quizFile);
             break;
         case 'text/csv':
         case 'text/plain':
             $txt = $file;
             break;
         default:
             throw new SyntaxException(_('Inteligenceωの辞書はテキストファイルかZIPファイルでなければなりません。'));
     }
     $filesCount = count($this->filenames) + 1;
     if ($filesCount > GenericDictionaryParser::MAX_FILES) {
         throw new SyntaxException(sprintf(_('アーカイブ中のファイル数は %1$s 個以下にしてください: 現在 %2$s 個'), GenericDictionaryParser::MAX_FILES, $filesCount));
     }
     $dictionary = new Dictionary($files ?? [], $this->filenames);
     $txt->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY);
     foreach ($txt as $line) {
         $this->parseLine($dictionary, $line);
     }
     $this->parseLine($dictionary);
     if (!$dictionary->getWords()) {
         throw new SyntaxException(_('正常に変換可能な行が見つかりませんでした。'));
     }
     if ($this->wholeText !== '') {
         $regard = $this->generateRegard();
         if ($regard) {
             $metaFields['@regard'] = $this->generateRegard();
         }
     }
     if (!is_null($title)) {
         $metaFields['@title'] = $title;
     } elseif (!is_null($filename)) {
         $titleFromFilename = $this->getTitleFromFilename($filename);
         if ($titleFromFilename) {
             $metaFields['@title'] = $titleFromFilename;
         }
     }
     if (isset($metaFields)) {
         $dictionary->setMetadata($metaFields);
     }
     return $dictionary;
 }