public function checkFile($file, array $config) { if (!isset($config['params'])) { return "Undefined MIME types."; } $type = file::getMimeType($file, isset($config['mime_magic']) ? $config['mime_magic'] : null); $type = substr($type, 0, strrpos($type, ";")); $mimes = $config['params']; if (substr($mimes, 0, 1) == "!") { $mimes = trim(substr($mimes, 1)); return in_array($type, explode(" ", $mimes)) ? "You can't upload such files." : true; } return !in_array($type, explode(" ", $mimes)) ? "You can't upload such files." : true; }
/** Cache a file. The $type parameter might define the MIME type of the file * or path to magic file to autodetect the MIME type. If you skip $type * parameter the method will try with the default magic file. Autodetection * of MIME type requires Fileinfo PHP extension used in file::getMimeType() * @param string $file * @param string $type * @param integer $expire * @param array $headers */ static function file($file, $type = null, $expire = null, array $headers = null) { $mtime = @filemtime($file); if ($mtime !== false) { self::checkMTime($mtime); } if ($type === null) { $magic = substr($type, 0, 1) == "/" || preg_match('/^[a-z]\\:/i', $type) ? $type : null; $type = file::getMimeType($file, $magic); if (!$type) { $type = null; } } self::content(@file_get_contents($file), $mtime, $type, $expire, $headers, false); }
protected function checkUploadedFileMime($targetFile) { $targetFileResult = false; if (!$targetFile || !file_exists($targetFile)) { return false; } $targetFileResult = $targetFile; if ($target_imagesize = @getimagesize($targetFile)) { if (!function_exists('image_type_to_extension')) { return false; } $renameResult = FALSE; $targetFilePathInfo = pathinfo($targetFile); // Get the EXPECTED EXTENSION from this MIME $targetMimeInteger = $target_imagesize[2]; $expectedExtension = @image_type_to_extension($targetMimeInteger); // get the existing MIME + MIME from the EXTENSION on this FILE ... $targetMimeString = $target_imagesize['mime']; $currentTargetMime = file::getMimeType($targetFile); if ($expectedExtension && $currentTargetMime != $targetMimeString) { // Ensure we use SHORTHAND JPG extensions .... if ($expectedExtension == ".jpeg" && $this->shortHandJpgExtensions) { $expectedExtension = ".jpg"; } // Rename the extension from whatever it was previously to a new extension ... $exptectedFileName = str_replace(".{$targetFilePathInfo['extension']}", $expectedExtension, $targetFile); $expectedFileResult = array('target' => basename($targetFile), 'destination' => basename($exptectedFileName)); // Testing this code ... // echo "<pre style='font-size:10px;'>"; // print_r($expectedFileResult); // echo "</pre>"; // die(); $renameResult = @rename($targetFile, $exptectedFileName); } if ($renameResult) { $targetFileResult = $exptectedFileName; } } return $targetFile; }