/** * Returns the suggested MIME type for an actual file. Using file-based heuristics * (data points in the ACTUAL file), it will utilize either the PECL FileInfo extension * OR the Magic MIME extension (if either are available) to determine the MIME type. If all * else fails, it will fall back to the basic GetMimeTypeForFilename() method. * * @param string $strFilePath the absolute file path of the ACTUAL file * @return string */ public static function GetMimeTypeForFile($strFilePath) { // Clean up the File Path and pull out the filename $strRealPath = realpath($strFilePath); if (!is_file($strRealPath)) { throw new QCallerException('File Not Found: ' . $strFilePath); } $strFilename = basename($strRealPath); $strToReturn = null; // First attempt using the PECL FileInfo extension if (class_exists('finfo')) { if (QMimeType::$MagicDatabaseFilePath) { $objFileInfo = new finfo(FILEINFO_MIME, QMimeType::$MagicDatabaseFilePath); } else { $objFileInfo = new finfo(FILEINFO_MIME); } $strToReturn = $objFileInfo->file($strRealPath); } // Next, attempt using the legacy MIME Magic extension if (!$strToReturn && function_exists('mime_content_type')) { $strToReturn = mime_content_type($strRealPath); } // Finally, use Qcodo's owns method for determining MIME type if (!$strToReturn) { $strToReturn = QMimeType::GetMimeTypeForFilename($strFilename); } if ($strToReturn) { return $strToReturn; } else { return QMimeType::_Default; } }
/** * Given a file at a temp file path location, this will save the file to the repository * and save the db row record. * @param string $strTemporaryFilePath the temporary file path to the file being saved to the repository * @param string $strFileName the name of the file that was originally uploaded * @return void */ public function SaveFile($strTemporaryFilePath, $strFileName) { // Update wiki file metadata $this->FileName = $strFileName; $this->FileSize = filesize($strTemporaryFilePath); $this->FileMime = QMimeType::GetMimeTypeForFilename($strFileName); // Save the Row $this->Save(); // Copy the File QApplication::MakeDirectory($this->GetFolder(), 0777); copy($strTemporaryFilePath, $this->GetPath()); chmod($this->GetPath(), 0666); }
protected function SaveWikiVersion() { // Validate it as a valid image file switch (QMimeType::GetMimeTypeForFile($this->flcImage->File)) { case QMimeType::Gif: case QMimeType::Jpeg: case QMimeType::Png: break; default: $this->flcImage->Warning = 'Must be a PNG, JPEG or GIF file'; return null; } // Create the Parameters for Save $objWikiImage = new WikiImage(); $objWikiImage->Description = trim($this->txtDescription->Text); $arrMethodParameters = array($this->flcImage->File); $objWikiVersion = $this->objWikiItem->CreateNewVersion(trim($this->txtTitle->Text), $objWikiImage, 'SaveFile', $arrMethodParameters, QApplication::$Person, null); return $objWikiVersion; }
public function __construct($strFilePath, $strSpecifiedMimeType = null, $strSpecifiedFileName = null) { // Set File Path if (!is_file(realpath($strFilePath))) { throw new QCallerException('File Not Found: ' . $strFilePath); } $this->strFilePath = realpath($strFilePath); // Set the File MIME Type -- if Explicitly Set, use it if ($strSpecifiedMimeType) { $this->strMimeType = $strSpecifiedMimeType; } else { $this->strMimeType = QMimeType::GetMimeTypeForFile($this->strFilePath); } // Set the File Name -- if explicitly set, use it if ($strSpecifiedFileName) { $this->strFileName = $strSpecifiedFileName; } else { $this->strFileName = basename($this->strFilePath); } // Read file into a Base64 Encoded Data Stream $strFileContents = file_get_contents($this->strFilePath, false); $this->strEncodedFileData = chunk_split(base64_encode($strFileContents)); }
<?php // For Searching using Zend Framework's Lucene Search library ini_set('include_path', '.:' . __INCLUDES__); require 'Zend/Search/Lucene.php'; // Pull from Session (if applicable) if (!QApplication::$CliMode) { QApplication::InitializePerson(); } // Other Setup QMimeType::$MagicDatabaseFilePath = MIME_MAGIC_PATH;