本文整理汇总了PHP中yii\helpers\FileHelper::getMimeType方法的典型用法代码示例。如果您正苦于以下问题:PHP FileHelper::getMimeType方法的具体用法?PHP FileHelper::getMimeType怎么用?PHP FileHelper::getMimeType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\helpers\FileHelper
的用法示例。
在下文中一共展示了FileHelper::getMimeType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getFromUrl
/**
* Returns UploadFile created from url
* Notice that this file cannot be saved by move_uploaded_file
* @param $url
* @throws \yii\base\InvalidConfigException
*/
public static function getFromUrl($url)
{
$tmpFile = null;
if (static::isExternalUrl($url)) {
//External url
$tmpFile = static::downloadToTmp($url);
} else {
//File must be in static folder
$staticPath = Yii::getAlias('@static/');
$path = str_replace(Yii::getAlias('@staticUrl/'), Yii::getAlias('@static/'), Yii::getAlias($url), $count);
//If we can replace static url to path
if ($count > 0) {
//Check staticPath after normalize
$path = FileHelper::normalizePath($path);
if (strpos($path, $staticPath) === 0) {
if (file_exists($path)) {
$tmpFile = tempnam(sys_get_temp_dir(), 'CURL');
if (!copy($path, $tmpFile)) {
$tmpFile = null;
}
}
}
}
}
if ($tmpFile) {
return new static(['name' => basename($url), 'tempName' => $tmpFile, 'type' => FileHelper::getMimeType($tmpFile), 'size' => filesize($tmpFile), 'error' => UPLOAD_ERR_OK]);
}
return null;
}
示例2: toJson
/**
* @return string
*/
public function toJson()
{
$toJson = [];
foreach (FileHelper::findFiles($this->getPath()) as $this->_file) {
$toJson = ArrayHelper::merge($toJson, [['title' => $this->normalizeFilename(), 'name' => FileHelper::getMimeType($this->_file), 'link' => $this->getUrl(), 'size' => $this->getSize()]]);
}
return Json::encode($toJson);
}
示例3: getImageFormat
private function getImageFormat($fn)
{
$f = FileHelper::getMimeType($fn);
if ($f === null) {
throw new NotSupportedException("ImageBehavior - Неизвестный тип файла");
}
$this->_format = str_replace('image/', '', $f);
}
示例4: determineExtension
/**
* @param $content
*
* @return mixed
* @throws \yii\base\InvalidConfigException
*/
protected function determineExtension($content)
{
$filename = \Yii::getAlias('@runtime/' . uniqid());
file_put_contents($filename, $content);
$mime = FileHelper::getMimeType($filename);
$extensions = FileHelper::getExtensionsByMimeType($mime);
unlink($filename);
return ArrayHelper::getValue($extensions, max(count($extensions) - 1, 0));
}
示例5: init
/**
* @inheritdoc
*/
public function init()
{
$this->tempName = $this->flowConfig->getTempDir() . DIRECTORY_SEPARATOR . $this->tempName;
$this->error = file_exists($this->tempName) ? UPLOAD_ERR_OK : UPLOAD_ERR_NO_FILE;
if ($this->error === UPLOAD_ERR_OK) {
$this->type = FileHelper::getMimeType($this->tempName);
$this->size = filesize($this->tempName);
}
}
示例6: upload
public function upload()
{
// $this->getAttributes()
$mime_type = \yii\helpers\FileHelper::getMimeType($this->file->tempName);
if ($this->validate($mime_type)) {
$this->file_path = 'uploads/' . $this->file->baseName . '.' . $this->file->extension;
return $this->file->saveAs($this->file_path);
} else {
return false;
}
}
示例7: createFromPath
/**
* Create file from path
*
* @param string $path Path in filesystem or URL
* @param int $ownerId
* @param int $ownerType
* @param bool $saveAfterUpload Save the file immediately after upload
* @param bool $protected File is protected?
* @return \rkit\filemanager\models\File|bool
*/
public function createFromPath($path, $ownerId = -1, $ownerType = -1, $saveAfterUpload = false, $protected = false)
{
$tempfile = tempnam(sys_get_temp_dir(), 'FMR');
if ($filecontent = @file_get_contents($path)) {
file_put_contents($tempfile, $filecontent);
$pathInfo = pathinfo($path);
$file = new File(['tmp' => true, 'owner_id' => $ownerId, 'owner_type' => $ownerType, 'size' => filesize($tempfile), 'mime' => FileHelper::getMimeType($tempfile), 'title' => $pathInfo['filename'], 'name' => File::generateName($pathInfo['extension']), 'protected' => $protected]);
return $file->saveToTmp($tempfile, $saveAfterUpload, false);
} else {
throw new InvalidValueException('Unable to create from `' . $path . '`');
}
}
示例8: uploadFile
/**
* Uploads the file into S3 in that bucket.
*
* @param string $filePath Full path of the file. Can be from tmp file path.
* @param string $fileName Filename to save this file into S3. May include directories.
* @param bool $bucket Override configured bucket.
* @return bool|string The S3 generated url that is publicly-accessible.
*/
public function uploadFile($filePath, $fileName, $bucket = false)
{
if (!$bucket) {
$bucket = $this->bucket;
}
try {
$result = $this->_client->putObject(['ACL' => 'public-read', 'Bucket' => $bucket, 'Key' => $fileName, 'SourceFile' => $filePath, 'ContentType' => \yii\helpers\FileHelper::getMimeType($filePath)]);
return $result->get('ObjectURL');
} catch (\Exception $e) {
return false;
}
}
示例9: makeUploadedFile
/**
* Create manually UploadedFile instance by file path
*
* @param string $path file path
* @return UploadedFile
*/
private function makeUploadedFile($path)
{
$tmpFile = tempnam(sys_get_temp_dir(), 'app');
file_put_contents($tmpFile, file_get_contents($path));
$uploadedFile = new UploadedFile();
$uploadedFile->name = pathinfo($path, PATHINFO_BASENAME);
$uploadedFile->tempName = $tmpFile;
$uploadedFile->type = FileHelper::getMimeType($tmpFile);
$uploadedFile->size = filesize($tmpFile);
$uploadedFile->error = 0;
return $uploadedFile;
}
示例10: createFromUploadedFile
public static function createFromUploadedFile(UploadedFile $file)
{
$upload = new static();
$upload->mimetype = FileHelper::getMimeType($file->tempName);
$upload->checksum = hash_file('sha256', $file->tempName);
$upload->filename = $file->getBaseName() . '.' . $file->getExtension();
$upload->filesize = $file->size;
$upload->createContainerDir();
$file->SaveAs($upload->getContainerDir() . '/' . $upload->filename);
$upload->save();
return $upload;
}
示例11: getInitialPreview
public function getInitialPreview()
{
$initialPreview = [];
$userTempDir = $this->getModule()->getUserDirPath();
foreach (FileHelper::findFiles($userTempDir) as $file) {
if (substr(FileHelper::getMimeType($file), 0, 5) === 'image') {
$initialPreview[] = Html::img(['/attachments/file/download-temp', 'filename' => basename($file)], ['class' => 'file-preview-image']);
} else {
$initialPreview[] = Html::beginTag('div', ['class' => 'file-preview-other']) . Html::beginTag('h2') . Html::tag('i', '', ['class' => 'glyphicon glyphicon-file']) . Html::endTag('h2') . Html::endTag('div');
}
}
foreach ($this->getFiles() as $file) {
if (substr($file->mime, 0, 5) === 'image') {
$initialPreview[] = Html::img($file->getUrl(), ['class' => 'file-preview-image']);
} else {
$initialPreview[] = Html::beginTag('div', ['class' => 'file-preview-other']) . Html::beginTag('h2') . Html::tag('i', '', ['class' => 'glyphicon glyphicon-file']) . Html::endTag('h2') . Html::endTag('div');
}
}
return $initialPreview;
}
示例12: rules
/**
* @inheritdoc
*/
public function rules()
{
return [[['file'], 'required'], [['file'], 'file', 'skipOnEmpty' => false], [['uploadPath'], 'required', 'when' => function ($obj) {
return empty($obj->filename);
}], [['name', 'size'], 'default', 'value' => function ($obj, $attribute) {
return $obj->file->{$attribute};
}], [['type'], 'default', 'value' => function () {
return FileHelper::getMimeType($this->file->tempName);
}], [['filename'], 'default', 'value' => function () {
$key = md5(microtime() . $this->file->name);
$base = Yii::getAlias($this->uploadPath);
if ($this->directoryLevel > 0) {
for ($i = 0; $i < $this->directoryLevel; ++$i) {
if (($prefix = substr($key, $i + $i, 2)) !== false) {
$base .= DIRECTORY_SEPARATOR . $prefix;
}
}
}
return $base . DIRECTORY_SEPARATOR . "{$key}_{$this->file->name}";
}], [['size'], 'integer'], [['name'], 'string', 'max' => 64], [['type'], 'string', 'max' => 32], [['filename'], 'string', 'max' => 256]];
}
示例13: validateExtension
public function validateExtension($attribute, $params)
{
$file = $this->{$attribute};
$pathinfo = pathinfo(mb_strtolower($file->name, 'utf-8'));
if (!empty($pathinfo['extension'])) {
$extension = $pathinfo['extension'];
} else {
return false;
}
if ($this->checkExtensionByMimeType) {
$mimeType = FileHelper::getMimeType($file->tempName, null, false);
if ($mimeType === null) {
return false;
}
if (!FileHelper::getMimeTypeByExtension($file)) {
return false;
}
}
if (!in_array($extension, $this->extensions, true)) {
return false;
}
return true;
}
示例14: save
/**
* Saves UploadedFile from fileData if exists and puts in it FileInfo
* @return bool
*/
public function save()
{
if (empty($this->uploadedFile)) {
return $this->fileInfo ? true : false;
}
$file = $this->uploadedFile;
$fileHash = md5_file($file->tempName);
// just in case if file already exists
$existingFileInfo = FileInfo::findOne(['hash' => $fileHash]);
if ($existingFileInfo) {
$this->fileInfo = $existingFileInfo;
return true;
}
$filePath = Yii::getAlias('@files/' . $fileHash . '.' . $file->extension);
if (!$file->saveAs($filePath)) {
return false;
}
$mimeType = MimeType::findOne(['name' => FileHelper::getMimeType($filePath)]);
$fileInfo = new FileInfo(['filePath' => $filePath, 'originalName' => $file->name, 'hash' => $fileHash, 'mimeTypeId' => $mimeType->id, 'size' => $file->size]);
$result = $fileInfo->save();
$this->fileInfo = $result ? $fileInfo : null;
return $result;
}
示例15: upload
/**
* Загрузка файла. При успехе создание и сохранение модели Attachment
* @return boolean
*/
public function upload()
{
$ext = $this->file->extension;
$origName = $this->file->baseName . '.' . $ext;
$size = $this->file->size;
$fileName = md5(uniqid()) . '.' . $ext;
$path = $this->getUploadDir() . '/' . $fileName;
if ($this->file->saveAs($path)) {
// создаем и сохраняем модель Attachment
$attachment = new Attachment();
$attachment->document_id = $this->documentID;
$attachment->origname = $origName;
$attachment->filename = $fileName;
$attachment->mimetype = FileHelper::getMimeType($path);
$attachment->size = $size;
if ($attachment->save()) {
return true;
} else {
// если ничего не получилось, удаляем загруженный файл
unlink($path);
}
}
return false;
}