function __construct($interface, $files_directory)
 {
     $field = filter_input(INPUT_POST, 'field', FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
     // Check for file upload.
     if ($field === NULL || empty($_FILES) || !isset($_FILES['file'])) {
         return;
     }
     $this->interface = $interface;
     // Create a new result object.
     $this->result = new stdClass();
     // Set directory.
     $this->files_directory = $files_directory;
     // Create the temporary directory if it doesn't exist.
     $dirs = array('', '/files', '/images', '/videos', '/audios');
     foreach ($dirs as $dir) {
         if (!H5PCore::dirReady($this->files_directory . $dir)) {
             $this->result->error = $this->interface->t('Unable to create directory.');
             return;
         }
     }
     // Get the field.
     $this->field = json_decode($field);
     if (function_exists('finfo_file')) {
         $finfo = finfo_open(FILEINFO_MIME_TYPE);
         $this->type = finfo_file($finfo, $_FILES['file']['tmp_name']);
         finfo_close($finfo);
     } elseif (function_exists('mime_content_type')) {
         // Deprecated, only when finfo isn't available.
         $this->type = mime_content_type($_FILES['file']['tmp_name']);
     } else {
         $this->type = $_FILES['file']['type'];
     }
     $this->extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
     $this->size = $_FILES['file']['size'];
 }
예제 #2
0
 public function download()
 {
     if (!$this->file_path) {
         $file = explode("/", $this->file_path);
         throw new Exception("Error finding file {$file[4]}.");
     } else {
         $finfo = finfo_open();
         $this->fileinfo = finfo_file($finfo, $this->file_path, FILEINFO_MIME);
         finfo_close($finfo);
         try {
             $this->sendHeaders();
         } catch (exception $e) {
             throw new Exception($e->getMessage());
         }
     }
     flush();
     $this->_oldMaxExecTime = ini_get('max_execution_time');
     ini_set('max_execution_time', 0);
     $this->delay = 256 / $this->downloadRate * 1000;
     $file = fopen($this->file_path, "rb");
     while (!feof($file)) {
         echo fread($file, $this->downloadRate);
         //((($this->downloadRate)*1024)/$this->_tickRate)
         flush();
         usleep($this->delay);
     }
     fclose($file);
     ini_set('max_execution_time', $this->_oldMaxExecTime);
     return true;
     // file downloaded.
 }
예제 #3
0
 function mime_type($file_path)
 {
     $finfo = finfo_open(FILEINFO_MIME_TYPE);
     $mime_type = finfo_file($finfo, $file_path);
     finfo_close($finfo);
     return $mime_type;
 }
예제 #4
0
 /**
  * Registers all the files of a static directory as resources.
  *
  * @param string 	$path 		The absolute path on the server which contains the files
  * @param string 	$prefix 	A prefix to add to the name of the static files
  * @return Route
  */
 public function registerStaticDirectory($path)
 {
     $path = rtrim($path, '/\\');
     return $this->register('/{file}', 'get')->pattern('file', '([^\\.]{2,}.*|.)')->name($path)->before(function (&$file, &$isRightResource) use($path) {
         $file = $path . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $file);
         if (!file_exists($file) || is_dir($file)) {
             $isRightResource = false;
             return;
         }
     })->before(function ($file, $etagResponseFilter) {
         $etagResponseFilter->setEtag(md5($file . filemtime($file)));
     })->handler(function ($file, $response, $elapsedTime) {
         if (!extension_loaded('fileinfo')) {
             throw new \LogicException('The "fileinfo" extension must be activated');
         }
         $finfo = finfo_open(FILEINFO_MIME);
         $mime = finfo_file($finfo, $file);
         finfo_close($finfo);
         $pathinfo = pathinfo($file);
         if ($pathinfo['extension'] == 'css') {
             $mime = 'text/css';
         }
         if ($pathinfo['extension'] == 'js') {
             $mime = 'application/javascript';
         }
         if ($pathinfo['extension'] == 'svg') {
             $mime = 'image/svg+xml';
         }
         if (substr($mime, 0, 15) == 'application/xml' && ($pathinfo['extension'] == 'htm' || $pathinfo['extension'] == 'html')) {
             $mime = 'application/xhtml+xml';
         }
         $response->setHeader('Content-Type', $mime);
         $response->appendData(file_get_contents($file));
     });
 }
예제 #5
0
파일: S3.php 프로젝트: awwthentic1234/hey
 public static function upload($file, $path = "", $fileName = NULL, $_mime = NULL)
 {
     //----------------------------------------------------------
     //init var
     //----------------------------------------------------------
     $chk = array("bool" => true, 'result' => array(), "func" => "upload");
     //----------------------------------------------------------
     $finfo = finfo_open(FILEINFO_MIME_TYPE);
     $mime = finfo_file($finfo, $file);
     finfo_close($finfo);
     if (!is_null($_mime)) {
         $mime = $_mime;
     }
     //----------------------------------------------------------
     $client = S3Client::factory(array('credentials' => array('key' => S3::$key, 'secret' => S3::$secret)));
     //----------------------------------------------------------
     try {
         $chk['result'] = $client->putObject(['Bucket' => S3::$bucket, 'Key' => $path . (!is_null($fileName) ? $fileName : basename($file)), 'Body' => fopen($file, r), 'ACL' => 'public-read', 'ContentType' => $mime]);
     } catch (S3Exception $e) {
         $chk['bool'] = false;
         $chk['message'] = "upload to s3 bucket: " . S3::$bucket . " failed!!!";
     }
     //----------------------------------------------------------
     return $chk;
 }
예제 #6
0
 /**
  * Check file mime type
  * @access	public
  * @param 	string $name
  * @param 	string $path
  * @param 	string $type
  * @return 	bool
  */
 public function check($name, $path)
 {
     $extension = strtolower(substr($name, strrpos($name, '.') + 1));
     if (function_exists('finfo_open')) {
         if ($finfo = @finfo_open(FILEINFO_MIME_TYPE)) {
             if ($mimetype = @finfo_file($finfo, $path)) {
                 @finfo_close($finfo);
                 $mime = self::getMime($mimetype);
                 if ($mime) {
                     return in_array($extension, $mime);
                 }
             }
         }
     } else {
         if (function_exists('mime_content_type')) {
             if ($mimetype = @mime_content_type($path)) {
                 $mime = self::getMime($mimetype);
                 if ($mime) {
                     return in_array($extension, $mime);
                 }
             }
         }
     }
     // server doesn't support mime type check, let it through...
     return true;
 }
예제 #7
0
function forceDownload($file)
{
    //Check file exist or not
    if (file_exists($file)) {
        if (ini_get('zlib.output_compression')) {
            // required for IE
            ini_set('zlib.output_compression', 'Off');
        }
        // Get mine type of file.
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        // return mime type ala mimetype extension
        $mimeType = finfo_file($finfo, $file) . "\n";
        finfo_close($finfo);
        header('Expires: 0');
        header('Pragma: public');
        header('Cache-Control: private', false);
        header('Content-Type:' . $mimeType);
        header('Content-Disposition: attachment; filename="' . basename($file) . '"');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Content-Length: ' . filesize($file));
        header('Connection: close');
        readfile($file);
        exit;
    } else {
        return "File does not exist";
    }
}
예제 #8
0
 function get_file_mime_type($filename, $debug = false)
 {
     if (function_exists('finfo_open') && function_exists('finfo_file') && function_exists('finfo_close')) {
         $fileinfo = finfo_open(FILEINFO_MIME);
         $mime_type = finfo_file($fileinfo, $filename);
         finfo_close($fileinfo);
         if (!empty($mime_type)) {
             if (true === $debug) {
                 return array('mime_type' => $mime_type, 'method' => 'fileinfo');
             }
             return $mime_type;
         }
     }
     if (function_exists('mime_content_type')) {
         $mime_type = mime_content_type($filename);
         if (!empty($mime_type)) {
             if (true === $debug) {
                 return array('mime_type' => $mime_type, 'method' => 'mime_content_type');
             }
             return $mime_type;
         }
     }
     $mime_types = array('ai' => 'application/postscript', 'aif' => 'audio/x-aiff', 'aifc' => 'audio/x-aiff', 'aiff' => 'audio/x-aiff', 'asc' => 'text/plain', 'asf' => 'video/x-ms-asf', 'asx' => 'video/x-ms-asf', 'au' => 'audio/basic', 'avi' => 'video/x-msvideo', 'bcpio' => 'application/x-bcpio', 'bin' => 'application/octet-stream', 'bmp' => 'image/bmp', 'bz2' => 'application/x-bzip2', 'cdf' => 'application/x-netcdf', 'chrt' => 'application/x-kchart', 'class' => 'application/octet-stream', 'cpio' => 'application/x-cpio', 'cpt' => 'application/mac-compactpro', 'csh' => 'application/x-csh', 'css' => 'text/css', 'dcr' => 'application/x-director', 'dir' => 'application/x-director', 'djv' => 'image/vnd.djvu', 'djvu' => 'image/vnd.djvu', 'dll' => 'application/octet-stream', 'dms' => 'application/octet-stream', 'doc' => 'application/msword', 'dvi' => 'application/x-dvi', 'dxr' => 'application/x-director', 'eps' => 'application/postscript', 'etx' => 'text/x-setext', 'exe' => 'application/octet-stream', 'ez' => 'application/andrew-inset', 'flv' => 'video/x-flv', 'gif' => 'image/gif', 'gtar' => 'application/x-gtar', 'gz' => 'application/x-gzip', 'hdf' => 'application/x-hdf', 'hqx' => 'application/mac-binhex40', 'htm' => 'text/html', 'html' => 'text/html', 'ice' => 'x-conference/x-cooltalk', 'ief' => 'image/ief', 'iges' => 'model/iges', 'igs' => 'model/iges', 'img' => 'application/octet-stream', 'iso' => 'application/octet-stream', 'jad' => 'text/vnd.sun.j2me.app-descriptor', 'jar' => 'application/x-java-archive', 'jnlp' => 'application/x-java-jnlp-file', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'js' => 'application/x-javascript', 'kar' => 'audio/midi', 'kil' => 'application/x-killustrator', 'kpr' => 'application/x-kpresenter', 'kpt' => 'application/x-kpresenter', 'ksp' => 'application/x-kspread', 'kwd' => 'application/x-kword', 'kwt' => 'application/x-kword', 'latex' => 'application/x-latex', 'lha' => 'application/octet-stream', 'lzh' => 'application/octet-stream', 'm3u' => 'audio/x-mpegurl', 'man' => 'application/x-troff-man', 'me' => 'application/x-troff-me', 'mesh' => 'model/mesh', 'mid' => 'audio/midi', 'midi' => 'audio/midi', 'mif' => 'application/vnd.mif', 'mov' => 'video/quicktime', 'movie' => 'video/x-sgi-movie', 'mp2' => 'audio/mpeg', 'mp3' => 'audio/mpeg', 'mpe' => 'video/mpeg', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'mpga' => 'audio/mpeg', 'ms' => 'application/x-troff-ms', 'msh' => 'model/mesh', 'mxu' => 'video/vnd.mpegurl', 'nc' => 'application/x-netcdf', 'odb' => 'application/vnd.oasis.opendocument.database', 'odc' => 'application/vnd.oasis.opendocument.chart', 'odf' => 'application/vnd.oasis.opendocument.formula', 'odg' => 'application/vnd.oasis.opendocument.graphics', 'odi' => 'application/vnd.oasis.opendocument.image', 'odm' => 'application/vnd.oasis.opendocument.text-master', 'odp' => 'application/vnd.oasis.opendocument.presentation', 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', 'odt' => 'application/vnd.oasis.opendocument.text', 'ogg' => 'application/ogg', 'otg' => 'application/vnd.oasis.opendocument.graphics-template', 'oth' => 'application/vnd.oasis.opendocument.text-web', 'otp' => 'application/vnd.oasis.opendocument.presentation-template', 'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template', 'ott' => 'application/vnd.oasis.opendocument.text-template', 'pbm' => 'image/x-portable-bitmap', 'pdb' => 'chemical/x-pdb', 'pdf' => 'application/pdf', 'pgm' => 'image/x-portable-graymap', 'pgn' => 'application/x-chess-pgn', 'png' => 'image/png', 'pnm' => 'image/x-portable-anymap', 'ppm' => 'image/x-portable-pixmap', 'ppt' => 'application/vnd.ms-powerpoint', 'ps' => 'application/postscript', 'qt' => 'video/quicktime', 'ra' => 'audio/x-realaudio', 'ram' => 'audio/x-pn-realaudio', 'ras' => 'image/x-cmu-raster', 'rgb' => 'image/x-rgb', 'rm' => 'audio/x-pn-realaudio', 'roff' => 'application/x-troff', 'rpm' => 'application/x-rpm', 'rtf' => 'text/rtf', 'rtx' => 'text/richtext', 'sgm' => 'text/sgml', 'sgml' => 'text/sgml', 'sh' => 'application/x-sh', 'shar' => 'application/x-shar', 'silo' => 'model/mesh', 'sis' => 'application/vnd.symbian.install', 'sit' => 'application/x-stuffit', 'skd' => 'application/x-koan', 'skm' => 'application/x-koan', 'skp' => 'application/x-koan', 'skt' => 'application/x-koan', 'smi' => 'application/smil', 'smil' => 'application/smil', 'snd' => 'audio/basic', 'so' => 'application/octet-stream', 'spl' => 'application/x-futuresplash', 'src' => 'application/x-wais-source', 'stc' => 'application/vnd.sun.xml.calc.template', 'std' => 'application/vnd.sun.xml.draw.template', 'sti' => 'application/vnd.sun.xml.impress.template', 'stw' => 'application/vnd.sun.xml.writer.template', 'sv4cpio' => 'application/x-sv4cpio', 'sv4crc' => 'application/x-sv4crc', 'swf' => 'application/x-shockwave-flash', 'sxc' => 'application/vnd.sun.xml.calc', 'sxd' => 'application/vnd.sun.xml.draw', 'sxg' => 'application/vnd.sun.xml.writer.global', 'sxi' => 'application/vnd.sun.xml.impress', 'sxm' => 'application/vnd.sun.xml.math', 'sxw' => 'application/vnd.sun.xml.writer', 't' => 'application/x-troff', 'tar' => 'application/x-tar', 'tcl' => 'application/x-tcl', 'tex' => 'application/x-tex', 'texi' => 'application/x-texinfo', 'texinfo' => 'application/x-texinfo', 'tgz' => 'application/x-gzip', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'torrent' => 'application/x-bittorrent', 'tr' => 'application/x-troff', 'tsv' => 'text/tab-separated-values', 'txt' => 'text/plain', 'ustar' => 'application/x-ustar', 'vcd' => 'application/x-cdlink', 'vrml' => 'model/vrml', 'wav' => 'audio/x-wav', 'wax' => 'audio/x-ms-wax', 'wbmp' => 'image/vnd.wap.wbmp', 'wbxml' => 'application/vnd.wap.wbxml', 'wm' => 'video/x-ms-wm', 'wma' => 'audio/x-ms-wma', 'wml' => 'text/vnd.wap.wml', 'wmlc' => 'application/vnd.wap.wmlc', 'wmls' => 'text/vnd.wap.wmlscript', 'wmlsc' => 'application/vnd.wap.wmlscriptc', 'wmv' => 'video/x-ms-wmv', 'wmx' => 'video/x-ms-wmx', 'wrl' => 'model/vrml', 'wvx' => 'video/x-ms-wvx', 'xbm' => 'image/x-xbitmap', 'xht' => 'application/xhtml+xml', 'xhtml' => 'application/xhtml+xml', 'xls' => 'application/vnd.ms-excel', 'xml' => 'text/xml', 'xpm' => 'image/x-xpixmap', 'xsl' => 'text/xml', 'xwd' => 'image/x-xwindowdump', 'xyz' => 'chemical/x-xyz', 'zip' => 'application/zip');
     $ext = strtolower(array_pop(explode('.', $filename)));
     if (!empty($mime_types[$ext])) {
         if (true === $debug) {
             return array('mime_type' => $mime_types[$ext], 'method' => 'from_array');
         }
         return $mime_types[$ext];
     }
     if (true === $debug) {
         return array('mime_type' => 'application/octet-stream', 'method' => 'last_resort');
     }
     return 'application/octet-stream';
 }
예제 #9
0
파일: file.php 프로젝트: BillVGN/PortalPRP
	public static function getMime($file)
	{
		// Check if file is an image.
		$info = @getimagesize($file);

		if ($info)
		{
			$type = $info['mime'];
		}
		elseif (function_exists('finfo_open'))
		{
			// We have fileinfo.
			$finfo = finfo_open(FILEINFO_MIME_TYPE);
			$type  = finfo_file($finfo, $file);
			finfo_close($finfo);
		}
		elseif (function_exists('mime_content_type'))
		{
			// We have mime magic.
			$type = mime_content_type($file);
		}
		else
		{
			$type = false;
		}

		return $type;
	}
예제 #10
0
 /**
  * detect mimetype based on both filename and content
  *
  * @param string $path
  * @return string
  */
 public function detect($path)
 {
     if (@is_dir($path)) {
         // directories are easy
         return "httpd/unix-directory";
     }
     $mimeType = $this->detectPath($path);
     if ($mimeType === 'application/octet-stream' and function_exists('finfo_open') and function_exists('finfo_file') and $finfo = finfo_open(FILEINFO_MIME)) {
         $info = @strtolower(finfo_file($finfo, $path));
         finfo_close($finfo);
         if ($info) {
             $mimeType = substr($info, 0, strpos($info, ';'));
             return empty($mimeType) ? 'application/octet-stream' : $mimeType;
         }
     }
     $isWrapped = strpos($path, '://') !== false and substr($path, 0, 7) === 'file://';
     if (!$isWrapped and $mimeType === 'application/octet-stream' && function_exists("mime_content_type")) {
         // use mime magic extension if available
         $mimeType = mime_content_type($path);
     }
     if (!$isWrapped and $mimeType === 'application/octet-stream' && \OC_Helper::canExecute("file")) {
         // it looks like we have a 'file' command,
         // lets see if it does have mime support
         $path = escapeshellarg($path);
         $fp = popen("file -b --mime-type {$path} 2>/dev/null", "r");
         $reply = fgets($fp);
         pclose($fp);
         //trim the newline
         $mimeType = trim($reply);
         if (empty($mimeType)) {
             $mimeType = 'application/octet-stream';
         }
     }
     return $mimeType;
 }
function get_mime($file, $default = "application/octet-stream")
{
    if (function_exists("mime_content_type")) {
        $mime = mime_content_type($file);
        if (!empty($mime)) {
            return $mime;
        }
    }
    if (function_exists("finfo_file")) {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mime = finfo_file($finfo, $file);
        finfo_close($finfo);
        if (!empty($mime)) {
            return $mime;
        }
    }
    if (!stristr(ini_get("disable_functions"), "shell_exec")) {
        $file = escapeshellarg($file);
        $mime = shell_exec("file -bi " . $file);
        if (!empty($mime)) {
            return $mime;
        }
    }
    return $default;
}
예제 #12
0
파일: mime.php 프로젝트: 01J/bealtine
 /**
  * Check file mime type
  * @access	public
  * @param 	string $name
  * @param 	string $path
  * @param 	string $type
  * @return 	bool
  */
 public function check($name, $path)
 {
     $extension = strtolower(substr($name, strrpos($name, '.') + 1));
     $ms_x = array('docx', 'pptx', 'ppsx', 'xlsx', 'sldx', 'potx', 'xltx', 'dotx');
     if (function_exists('finfo_open')) {
         if ($finfo = @finfo_open(FILEINFO_MIME_TYPE)) {
             if ($mimetype = @finfo_file($finfo, $path)) {
                 @finfo_close($finfo);
                 // we can't validate these files...
                 if ($mimetype === 'application/zip' && in_array($extension, $ms_x)) {
                     return true;
                 }
                 if ($mime = self::getMime($mimetype)) {
                     return in_array($extension, $mime);
                 }
             }
         }
     } else {
         if (function_exists('mime_content_type')) {
             if ($mimetype = @mime_content_type($path)) {
                 // we can't validate these files...
                 if ($mimetype === 'application/zip' && in_array($extension, $ms_x)) {
                     return true;
                 }
                 if ($mime = self::getMime($mimetype)) {
                     return in_array($extension, $mime);
                 }
             }
         }
     }
     // server doesn't support mime type check, let it through...
     return true;
 }
예제 #13
0
파일: item.php 프로젝트: Tommar/vino2
 public function getMime()
 {
     $mimeType = false;
     if (is_file($this->file)) {
         if (function_exists('finfo_open')) {
             $finfo = finfo_open(FILEINFO_MIME_TYPE);
             $mimeType = finfo_file($finfo, $this->file);
             finfo_close($finfo);
         }
         if (!$mimeType && function_exists('mime_content_type')) {
             $mimeType = mime_content_type($this->file);
         }
         // 			if( !$mimeType && function_exists('exec') && function_exists('escapeshellarg') )
         // 			{
         // 				if( $cmdOut = trim(exec('file --mime-type -b ' . escapeshellarg( $this->file ))) )
         // 				{
         // 					$mimeType = $cmdOut;
         // 				}
         // 			}
         if (!$mimeType && function_exists('pathinfo') && ($pathinfo = pathinfo($this->file))) {
             $imagetypes = array('gif', 'jpeg', 'jpg', 'png', 'swf', 'psd', 'bmp', 'tiff', 'tif', 'jpc', 'jp2', 'jpx', 'jb2', 'swc', 'iff', 'wbmp', 'xbm', 'ico');
             if (in_array($pathinfo['extension'], $imagetypes) && getimagesize($this->file)) {
                 $imageinfo = getimagesize($this->file);
                 $mimeType = $imageinfo['mime'];
             }
         }
         if (!$mimeType) {
             $mimeType = 'application/octet-stream';
         }
     }
     return $mimeType;
 }
예제 #14
0
파일: assets.php 프로젝트: arribanz/live
function isPicture($file, $types = NULL)
{
    /* Detect mime content type */
    $mimeType = false;
    if (!$types) {
        $types = array('image/gif', 'image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png');
    }
    /* Try 4 different methods to determine the mime type */
    if (function_exists('finfo_open')) {
        $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
        $finfo = finfo_open($const);
        $mimeType = finfo_file($finfo, $file['tmp_name']);
        finfo_close($finfo);
    } elseif (function_exists('mime_content_type')) {
        $mimeType = mime_content_type($file['tmp_name']);
    } elseif (function_exists('exec')) {
        $mimeType = trim(exec('file -b --mime-type ' . escapeshellarg($file['tmp_name'])));
        if (!$mimeType) {
            $mimeType = trim(exec('file --mime ' . escapeshellarg($file['tmp_name'])));
        }
        if (!$mimeType) {
            $mimeType = trim(exec('file -bi ' . escapeshellarg($file['tmp_name'])));
        }
    }
    if (empty($mimeType) or $mimeType == 'regular file' or $mimeType == 'text/plain') {
        $mimeType = $file['type'];
    }
    /* For each allowed MIME type, we are looking for it inside the current MIME type */
    foreach ($types as $type) {
        if (strstr($mimeType, $type)) {
            return true;
        }
    }
    return false;
}
예제 #15
0
파일: File.php 프로젝트: inphinit/framework
 public static function isBinary($path)
 {
     if (false === is_readable($path)) {
         Exception::raise($path . ' is not readable', 2);
     }
     $size = filesize($path);
     if ($size < 2) {
         return false;
     }
     $data = file_get_contents($path, false, null, -1, 5012);
     if (false && function_exists('finfo_open')) {
         $finfo = finfo_open(FILEINFO_MIME_ENCODING);
         $encode = finfo_buffer($finfo, $data, FILEINFO_MIME_ENCODING);
         finfo_close($finfo);
         $data = null;
         return $encode === 'binary';
     }
     $buffer = '';
     for ($i = 0; $i < $size; ++$i) {
         if (isset($data[$i])) {
             $buffer .= sprintf('%08b', ord($data[$i]));
         }
     }
     $data = null;
     return preg_match('#^[0-1]+$#', $buffer) === 1;
 }
예제 #16
0
 private function fileMimeType($filePath)
 {
     $finfo = finfo_open(FILEINFO_MIME_TYPE);
     $type = finfo_file($finfo, $filePath);
     finfo_close($finfo);
     return $type;
 }
예제 #17
0
 public static function fileDownload($filepath, $test = false)
 {
     //prevent transverse above BROWSE_URL dir
     if (strpos(realpath($filepath), realpath(BROWSE_URL)) !== 0) {
         return false;
     }
     //for unit test, no need to download read file
     if ($test) {
         return true;
     }
     if (is_file($filepath)) {
         $size = filesize($filepath);
         if (function_exists('finfo_open')) {
             $finfo = finfo_open(FILEINFO_MIME_TYPE);
             $mimetype = finfo_file($finfo, $filepath);
             finfo_close($finfo);
         } else {
             $mimetype = 'application/octet-stream';
         }
         //clear all output buffer
         ob_end_clean();
         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
         header('Cache-Control: private', false);
         header("Content-Type: {$mimetype}");
         header("Content-Disposition: attachment; filename=\"" . basename($filepath) . '"');
         header("Content-length: " . $size);
         header("Content-Transfer-Encoding: binary");
         readfile($filepath);
     } else {
         return false;
     }
 }
예제 #18
0
파일: File.php 프로젝트: efueger/luya
 public function getMimeType($sourceFile)
 {
     $finfo = finfo_open(FILEINFO_MIME_TYPE);
     $mime = finfo_file($finfo, $sourceFile);
     finfo_close($finfo);
     return $mime;
 }
예제 #19
0
파일: Mime.php 프로젝트: sfie/pimcore
 /**
  * @param $file
  * @param null $filename
  * @return mixed|string
  * @throws \Exception
  */
 public static function detect($file, $filename = null)
 {
     if (!file_exists($file)) {
         throw new \Exception("File " . $file . " doesn't exist");
     }
     if (!$filename) {
         $filename = basename($file);
     }
     // check for an extension mapping first
     if ($filename) {
         $extension = \Pimcore\File::getFileExtension($filename);
         if (array_key_exists($extension, self::$extensionMapping)) {
             return self::$extensionMapping[$extension];
         }
     }
     // check with fileinfo, if there's no extension mapping
     $finfo = finfo_open(FILEINFO_MIME);
     $type = finfo_file($finfo, $file);
     finfo_close($finfo);
     if ($type !== false && !empty($type)) {
         if (strstr($type, ';')) {
             $type = substr($type, 0, strpos($type, ';'));
         }
         return $type;
     }
     // return default mime-type if we're unable to detect it
     return "application/octet-stream";
 }
예제 #20
0
파일: File.php 프로젝트: efaby/postulacion
 public function download($nombre)
 {
     $path = PATH_FILES . $nombre;
     $type = '';
     if (is_file($path)) {
         $size = filesize($path);
         if (function_exists('mime_content_type')) {
             $type = mime_content_type($path);
         } else {
             if (function_exists('finfo_file')) {
                 $info = finfo_open(FILEINFO_MIME);
                 $type = finfo_file($info, $path);
                 finfo_close($info);
             }
         }
         if ($type == '') {
             $type = "application/force-download";
         }
         header("Content-Type: {$type}");
         header("Content-Disposition: attachment; filename={$nombre}");
         header("Content-Transfer-Encoding: binary");
         header("Content-Length: " . $size);
         readfile($path);
     } else {
         die("El archivo no existe.");
     }
 }
예제 #21
0
 /**
  * Detects the MIME type of a given file
  * @param string $fileName The full path to the name whose MIME type you want to find out
  * @return string The MIME type, e.g. image/jpeg
  */
 static function getMimeType($fileName)
 {
     $mime = null;
     // Try fileinfo first
     if (function_exists('finfo_open')) {
         $finfo = finfo_open(FILEINFO_MIME);
         if ($finfo !== false) {
             $mime = finfo_file($finfo, $fileName);
             finfo_close($finfo);
         }
     }
     // Fallback to mime_content_type() if finfo didn't work
     if (is_null($mime) && function_exists('mime_content_type')) {
         $mime = mime_content_type($fileName);
     }
     // Final fallback, detection based on extension
     if (is_null($mime)) {
         $extension = self::getTypeIcon(getTypeIcon);
         if (array_key_exists($extension, self::$mimeMap)) {
             $mime = self::$mimeMap[$extension];
         } else {
             $mime = "application/octet-stream";
         }
     }
     return $mime;
 }
예제 #22
0
파일: fs.php 프로젝트: Rudi9719/lucid
 function mime_content_type($filename)
 {
     $finfo = finfo_open(FILEINFO_MIME);
     $mimetype = finfo_file($finfo, $filename);
     finfo_close($finfo);
     return $mimetype;
 }
예제 #23
0
 function getMIME($fname)
 {
     $finfo = finfo_open(FILEINFO_MIME_TYPE);
     $mime = finfo_file($finfo, $fname);
     finfo_close($finfo);
     return $mime;
 }
예제 #24
0
function getUploadedFile($name)
{
    $tmp_name = $_FILES[$name]['tmp_name'];
    $info = finfo_open(FILEINFO_MIME_TYPE);
    $mime_type = finfo_file($info, $tmp_name);
    finfo_close($info);
    $extension = '';
    switch ($mime_type) {
        case 'image/jpeg':
            $extension = 'jpg';
            break;
        case 'image/gif':
            $extension = 'gif';
            break;
        case 'image/png':
            $extension = 'png';
            break;
        default:
            $extension = '';
    }
    if ($extension != '') {
        $image_name = pathinfo($_FILES[$name]['name'])['filename'] . '.' . $extension;
        $image_file = 'img-uploads/' . $image_name;
        try {
            move_uploaded_file($tmp_name, $image_file);
        } catch (Exception $e) {
            print_r($e);
        }
    }
    return $image_name;
}
예제 #25
0
파일: Fso.class.php 프로젝트: noikiy/dophin
 /**
  * 获取文件的mime-type
  * @param string $file
  */
 public static function getMimeType($file)
 {
     $finfo = finfo_open(FILEINFO_MIME_TYPE);
     $type = finfo_file($finfo, $file);
     finfo_close($finfo);
     return $type;
 }
예제 #26
0
파일: File.php 프로젝트: moiseh/codegen
 public function getType()
 {
     $finfo = finfo_open(FILEINFO_MIME_TYPE);
     $type = finfo_file($finfo, $this->getFullPath());
     finfo_close($finfo);
     return $type;
 }
예제 #27
0
 /**
  * @desc Retrieve the corresponding MIME type, if one exists
  * @param String $file File Name (relative location such as "image_test.jpg" or full "http://site.com/path/to/image_test.jpg")
  * @return String $MIMEType - The type of the file passed in the argument
  */
 public function getMimeType($file = NULL)
 {
     if (is_file($file)) {
         /**
          * Attempts to retrieve file info from FINFO
          * If FINFO functions are not available then try to retrieve MIME type from pre-defined MIMEs
          * If MIME type doesn't exist, then try (as a last resort) to use the (deprecated) mime_content_type function
          * If all else fails, just return application/octet-stream
          */
         if (!function_exists("finfo_open")) {
             $extension = $this->getExtension($file);
             if (array_key_exists($extension, $this->mimeTypes)) {
                 return $this->mimeTypes[$extension];
             } else {
                 if (function_exists("mime_content_type")) {
                     $type = mime_content_type($file);
                     return !empty($type) ? $type : "application/octet-stream";
                 } else {
                     return "application/octet-stream";
                 }
             }
         } else {
             $finfo = finfo_open(FILEINFO_MIME);
             $MIMEType = finfo_file($finfo, $file);
             finfo_close($finfo);
             return $MIMEType;
         }
     } else {
         return "##INVALID_FILE##FILE=" . $file . "##";
     }
 }
예제 #28
0
/**
 *
 * @copyright  2010-2015 izend.org
 * @version    7
 * @link       http://www.izend.org
 */
function file_mime_type($file, $encoding = true)
{
    $mime = false;
    if (function_exists('finfo_file')) {
        $finfo = finfo_open(FILEINFO_MIME);
        $mime = @finfo_file($finfo, $file);
        finfo_close($finfo);
    } else {
        if (substr(PHP_OS, 0, 3) == 'WIN') {
            $mime = mime_content_type($file);
        } else {
            $file = escapeshellarg($file);
            $cmd = "file -iL {$file}";
            exec($cmd, $output, $r);
            if ($r == 0) {
                $mime = substr($output[0], strpos($output[0], ': ') + 2);
            }
        }
    }
    if (!$mime) {
        return false;
    }
    if ($encoding) {
        return $mime;
    }
    return substr($mime, 0, strpos($mime, '; '));
}
예제 #29
0
파일: Utils.php 프로젝트: Konosprod/ciconia
 public static function getMimetype($path)
 {
     $finfo = finfo_open(FILEINFO_MIME_TYPE);
     $mime = finfo_file($finfo, $path);
     finfo_close($finfo);
     return $mime;
 }
예제 #30
0
파일: Editor.php 프로젝트: gotcms/gotcms
 /**
  * Save upload editor
  *
  * @return void
  */
 public function save()
 {
     $fileClass = new File();
     $fileClass->load($this->getProperty(), $this->getDatatype()->getDocument());
     $post = $this->getRequest()->getPost();
     $values = $post->get($this->getName(), array());
     $parameters = $this->getConfig();
     $arrayValues = array();
     if (!empty($values) and is_array($values)) {
         foreach ($values as $idx => $value) {
             if (empty($value['name'])) {
                 continue;
             }
             $file = $fileClass->getPath() . '/' . $value['name'];
             if (file_exists($file)) {
                 $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
                 $finfo = finfo_open($const);
                 // return mimetype extension
                 if (!in_array(finfo_file($finfo, $file), $parameters['mime_list'])) {
                     unlink($file);
                 } else {
                     $fileInfo = @getimagesize($file);
                     $arrayValues[] = array('value' => $value['name'], 'width' => empty($fileInfo[0]) ? 0 : $fileInfo[0], 'height' => empty($fileInfo[1]) ? 0 : $fileInfo[1], 'html' => empty($fileInfo[2]) ? '' : $fileInfo[2], 'mime' => empty($fileInfo['mime']) ? '' : $fileInfo['mime']);
                 }
                 finfo_close($finfo);
             }
         }
         $returnValues = serialize($arrayValues);
     }
     $this->setValue(empty($returnValues) ? null : $returnValues);
 }