本文整理汇总了PHP中finfo::file方法的典型用法代码示例。如果您正苦于以下问题:PHP finfo::file方法的具体用法?PHP finfo::file怎么用?PHP finfo::file使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类finfo
的用法示例。
在下文中一共展示了finfo::file方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: applyFileProperties
/**
* @param FileJudgementBuilder $fileJudgementBuilder
* @return FileJudgementBuilder
*/
protected function applyFileProperties(FileJudgementBuilder $fileJudgementBuilder)
{
$this->actualFileSize = filesize($this->filepath);
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$this->actualMediaType = explode("/", $finfo->file($this->filepath))[0];
$this->actualMediaTypeSubtype = explode("/", $finfo->file($this->filepath))[1];
$fileJudgementBuilder->setFileSize($this->actualFileSize);
$fileJudgementBuilder->setMediaType($this->actualMediaType);
$fileJudgementBuilder->setMediaTypeSubtype($this->actualMediaTypeSubtype);
return $fileJudgementBuilder;
}
示例2: setFileContentFromFilesystem
/**
* Set the content for this file from the given filename.
* Calls file_get_contents with the given filename
*
* @param string $filename name of the file which contents should be used
*/
public function setFileContentFromFilesystem($filename)
{
$this->getContent();
$stream = fopen($filename, 'rb');
if (!$stream) {
throw new \RuntimeException("File '{$filename}' not found");
}
$this->content->setData($stream);
$this->content->setLastModified(new \DateTime('@' . filemtime($filename)));
$finfo = new \finfo();
$this->content->setEncoding($finfo->file($filename, FILEINFO_MIME_ENCODING));
$this->content->setMimeType($finfo->file($filename, FILEINFO_MIME_TYPE));
$this->updateDimensionsFromContent();
}
示例3: mime
/**
* Attempt to get the mime type from a file. This method is horribly
* unreliable, due to PHP being horribly unreliable when it comes to
* determining the mime type of a file.
*
* $mime = File::mime($file);
*
* @param string $filename file name or path
* @return string mime type on success
* @return false on failure
*/
public static function mime($filename)
{
// Get the complete path to the file
$filename = realpath($filename);
// Get the extension from the filename
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if (preg_match('/^(?:jpe?g|png|[gt]if|bmp|swf)$/', $extension)) {
// Use getimagesize() to find the mime type on images
$file = getimagesize($filename);
if (!empty($file['mime'])) {
return $file['mime'];
}
}
if (function_exists('finfo_open')) {
$info = new finfo(FILEINFO_MIME_TYPE);
return $info->file($filename);
}
if (ini_get('mime_magic.magicfile') && function_exists('mime_content_type')) {
// mime_content_type is deprecated since PHP 5.3.0
return mime_content_type($filename);
}
if ($extension) {
return static::mimeForExtension($extension);
}
// Unable to find the mime-type
return false;
}
示例4: __construct
/**
* Attachment constructor.
*
* @param string $filePath
* @param string|null $type
* @throws MessageException
*/
public function __construct(string $filePath, string $type = null)
{
// Check if file exists and is readable
if (!@is_readable($filePath)) {
throw MessageException::attachmentUnreadable(__METHOD__, $filePath);
}
$this->path = $filePath;
// Save file path
$this->type = $type;
// Content type (if specified)
$this->name = basename($this->path);
$this->id = null;
$this->disposition = "attachment";
// Check if content type is not explicit
if (!$this->type) {
// Check if "fileinfo" extension is loaded
if (extension_loaded("fileinfo")) {
$fileInfo = new \finfo(FILEINFO_MIME_TYPE);
$this->type = $fileInfo->file($this->path);
}
if (!$this->type) {
$this->type = self::fileType($this->name);
}
}
}
示例5: isValid
/**
* Defined by Zend_Validate_Interface
*
* Returns true if the mimetype of the file does not matche the given ones. Also parts
* of mimetypes can be checked. If you give for example "image" all image
* mime types will not be accepted like "image/gif", "image/jpeg" and so on.
*
* @param string $value Real file to check for mimetype
* @param array $file File data from Zend_File_Transfer
* @return boolean
*/
public function isValid($value, $file = null)
{
// Is file readable ?
require_once 'Zend/Loader.php';
if (!Zend_Loader::isReadable($value)) {
return $this->_throw($file, self::NOT_READABLE);
}
if ($file !== null) {
if (class_exists('finfo', false) && defined('MAGIC')) {
$mime = new finfo(FILEINFO_MIME);
$this->_type = $mime->file($value);
unset($mime);
} elseif (function_exists('mime_content_type') && ini_get('mime_magic.magicfile')) {
$this->_type = mime_content_type($value);
} else {
$this->_type = $file['type'];
}
}
if (empty($this->_type)) {
return $this->_throw($file, self::NOT_DETECTED);
}
$mimetype = $this->getMimeType(true);
if (in_array($this->_type, $mimetype)) {
return $this->_throw($file, self::FALSE_TYPE);
}
$types = explode('/', $this->_type);
$types = array_merge($types, explode('-', $this->_type));
foreach ($mimetype as $mime) {
if (in_array($mime, $types)) {
return $this->_throw($file, self::FALSE_TYPE);
}
}
return true;
}
示例6: download
/**
* Try download file
*/
public function download()
{
if ($this->fileInfo instanceof \SplFileInfo) {
if ($this->fileInfo->isFile()) {
if (is_array($this->validatorExtension)) {
if (!in_array($this->fileInfo->getExtension(), $this->validatorExtension)) {
throw new \Exception("Extensão invalida!");
}
}
if (!$this->fileInfo->isReadable()) {
throw new \Exception("O Arquivo não pode ser lido!");
}
if (is_null($this->newName)) {
$this->setNewName($this->fileInfo->getBasename());
}
$finfo = new \finfo();
header("Content-Type: {$finfo->file($this->fileInfo->getRealPath(), FILEINFO_MIME)}");
header("Content-Length: {$this->fileInfo->getSize()}");
header("Content-Disposition: attachment; filename={$this->newName}");
readfile($this->fileInfo->getRealPath());
} else {
throw new \Exception("Por favor, adicione um arquivo valido!");
}
} else {
throw new \Exception("Por favor, adicione o arquivo primeiro!");
}
}
示例7: send
/**
* @param $filepath
* @param string|null $type set mime type
* @return mixed
*/
public function send($filepath, $type = null)
{
if (empty($type)) {
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$type = $finfo->file($filepath);
}
$getData = array();
$url = 'upload/';
$postData = array('|file[]' => '@' . $filepath . ";type=" . trim($type, ';') . ";");
$addToken = true;
if (!is_string($filepath)) {
throw new Exception('$file_url doit être une chaîne de caractères.');
}
$this->getClient()->buildURL($url, $getData, $postData, $addToken);
$ch = curl_init();
curl_setopt_array($ch, array(CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLINFO_HEADER_OUT => true, CURLOPT_HEADER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $postData));
$res = explode("\r\n\r\n", curl_exec($ch));
$info = curl_getinfo($ch);
$info = array('request' => array('url' => $info['url'], 'header' => curl_getinfo($ch, CURLINFO_HEADER_OUT), 'post_data' => json_encode($postData), 'content_type' => $info['content_type']), 'response' => array('header' => $res[0], 'http_code' => $info['http_code'], 'data' => json_decode($res[1], true)));
$this->getClient()->setLastRequest($info['request']);
$this->getClient()->setLastResponse($info['response']);
if ($info['response']['http_code'] != '200') {
$this->getClient()->getLogger()->critical('CURL Request failed');
if ($this->getClient()->isDebugMode()) {
throw new Exception('CURL Request failed.');
}
}
curl_close($ch);
$jsonData = json_decode($res[2], true);
return isset($jsonData['data'][0]) ? $jsonData['data'][0] : null;
}
示例8: checkForImages
public function checkForImages($arrFiles)
{
global $GLOBALS;
if (isset($GLOBALS['TL_CONFIG']['krakenIo_enable']) && $GLOBALS['TL_CONFIG']['krakenIo_enable'] == true) {
if (isset($GLOBALS['TL_CONFIG']['krakenIo_apiKey']) && isset($GLOBALS['TL_CONFIG']['krakenIo_apiSecret'])) {
$getMimeType = new \finfo(FILEINFO_MIME_TYPE);
$allowedTypes = array('image/jpeg', 'image/png');
$krakenIoApi = new KrakenIoApi($GLOBALS['TL_CONFIG']['krakenIo_apiKey'], $GLOBALS['TL_CONFIG']['krakenIo_apiSecret']);
foreach ($arrFiles as $file) {
if (in_array($getMimeType->file(TL_ROOT . '/' . $file), $allowedTypes)) {
if (!strpos('assets', $file)) {
$params = array('file' => TL_ROOT . '/' . $file, 'wait' => true);
if (isset($GLOBALS['TL_CONFIG']['krakenIo_enable']) && $GLOBALS['TL_CONFIG']['krakenIo_enable'] == true) {
$params['lossy'] = true;
}
$krakenIoApiResponse = $krakenIoApi->upload($params);
$this->parseKrakenIoResponse($krakenIoApiResponse, $file);
}
}
}
} else {
\System::log($GLOBALS['TL_LANG']['ERR']['krakenIo_404'], 'krakenIoInterface parseKrakenIoResponse()', TL_ERROR);
}
}
}
示例9: mime
/**
* @param \SplFileInfo $file
* @return string
*/
public function mime(\SplFileInfo $file)
{
if (empty($this->_fInfo)) {
$this->_fInfo = new \finfo(FILEINFO_MIME_TYPE);
}
return $this->_fInfo->file($file->getRealPath());
}
示例10: upload
public static function upload($data, $file)
{
if (isset($file)) {
$tmpFile = $_FILES["file"]["tmp_name"];
$idArticle = $data['idArticle'];
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($file['tmp_name']);
switch ($mime) {
case 'image/jpeg':
$extension = ".jpg";
$destination = IMAGES_PATH;
break;
case 'image/png':
$extension = ".png";
$destination = IMAGES_PATH;
break;
case 'image/gif':
$extension = ".gif";
$destination = IMAGES_PATH;
break;
case 'application/pdf':
$extension = ".pdf";
$destination = DOCS_PATH;
break;
default:
throw new UploadException("Ce n'est pas le bon type de fichier");
break;
}
$fileName = !empty($nameOfFile) ? uniqid('articleFile_' . $idArticle . '_') : '';
$chemin = $destination . $fileName . $extension;
list($width, $height) = getimagesize($tmpFile);
}
}
示例11: __construct
public function __construct($file, $validate)
{
parent::__construct();
if (!is_string($file) || (!is_readable($file)))
{
throw new DocBlox_Reflection_Exception('The given file should be a string, should exist on the filesystem and should be readable');
}
if ($validate)
{
exec('php -l '.escapeshellarg($file), $output, $result);
if ($result != 0)
{
throw new DocBlox_Reflection_Exception('The given file could not be interpreted as it contains errors: '.implode(PHP_EOL, $output));
}
}
$this->filename = $file;
$this->name = $this->filename;
$contents = file_get_contents($file);
// detect encoding and transform to UTF-8
$info = new finfo();
$mime = $info->file($file, FILEINFO_MIME);
$mime_info = explode('=', $mime);
if (strtolower($mime_info[1]) != 'utf-8')
{
$contents = iconv($mime_info[1], 'UTF-8', $contents);
}
$this->contents = $contents;
$this->setHash(filemtime($file));
}
示例12: getMime
/**
* checks mime type of file
* @param string $file
* @return bool
* @access private
*/
public static function getMime($file)
{
$mime = null;
if (class_exists("finfo")) {
$finfo = new finfo(FILEINFO_MIME);
$mime = $finfo->file($file);
}
if (function_exists("finfo_open")) {
$finfo = finfo_open(FILEINFO_MIME);
$mime = finfo_file($finfo, $file);
finfo_close($finfo);
} else {
$fp = fopen($file, 'r');
$str = fread($fp, 4);
fclose($fp);
switch ($str) {
case "ÿØÿà":
$mime = 'image/jpeg';
break;
case "‰PNG":
$mime = 'image/png';
break;
case 'GIF8':
$mime = 'image/gif';
break;
default:
trigger_error("Tell the guy behind the levers of i² to see {$str} as a valid file. Thank you //Sincerely, i ", E_USER_ERROR);
$mime = $str;
}
}
if (preg_match("/[a-zA-Z]+\\/[a-zA-Z]+.+/", $mime)) {
$mime = preg_replace("/([a-zA-Z]+\\/[a-zA-Z]+)(.+)/", "\$1", $mime);
}
return $mime;
}
示例13: __construct
/**
*
* @param string $filepath
*/
public function __construct($filepath)
{
$this->filepath = $filepath;
$this->basename = basename($filepath);
$finfo = new \finfo(FILEINFO_MIME);
list($this->mime, $this->charset) = explode('; ', $finfo->file($filepath));
}
示例14: execute
function execute($temp_name, $allow_existing_file = false)
{
if ($this->_autoRename) {
$this->_cleanFilename();
$this->_path_target = $this->_findSafeFilename();
}
if (!isset($this->_whitelist[$this->_file_extension])) {
die('File extension is not permitted.');
}
$finfo = new finfo(FILEINFO_MIME);
$mime_type = $finfo->file($temp_name);
$mime_type = explode(';', $mime_type);
$mime_type = $mime_type[0];
if ($mime_type != $this->_whitelist[$this->_file_extension]) {
die('File type/extension combination not permitted for security reasons.');
}
if (is_uploaded_file($temp_name)) {
if (!move_uploaded_file($temp_name, $this->_path_target)) {
return false;
}
} elseif ($allow_existing_file) {
if (!rename($temp_name, $this->_path_target)) {
return false;
}
} else {
return false;
}
chmod($this->_path_target, 0755);
AMP_s3_save($this->_path_target);
AMP_lookup_clear_cached('downloads');
return true;
}
示例15: uploadFile
function uploadFile($uploadUrl, $filePath, $blockSize = 1024)
{
$filePath = realpath($filePath);
/* determing file mime-type */
$finfo = new finfo(FILEINFO_MIME_TYPE);
$contentType = $finfo->file($filePath);
$fileSize = filesize($filePath);
$fp = fopen($filePath, "rb");
/* Perform chunked file uploading */
for ($i = 0; $i * $blockSize < $fileSize; $i++) {
if (($i + 1) * $blockSize > $fileSize) {
$size = $fileSize - $i * $blockSize;
} else {
$size = $blockSize;
}
$data = fread($fp, $size);
$opts = array('http' => array('method' => 'PUT', 'header' => array('Content-type: ' . $contentType, "X-File-Chunk: {$i}"), 'content' => $data));
$context = stream_context_create($opts);
$uploadResponse = json_decode(file_get_contents($uploadUrl, false, $context));
if (!$uploadResponse->ok) {
return (object) array("ok" => FALSE, "errorMessage" => $uploadResponse->errorMessage);
}
}
return (object) array("ok" => TRUE, "numberOfChunks" => $fileSize / $blockSize + 1);
}