本文整理汇总了PHP中yii\web\UploadedFile::getExtension方法的典型用法代码示例。如果您正苦于以下问题:PHP UploadedFile::getExtension方法的具体用法?PHP UploadedFile::getExtension怎么用?PHP UploadedFile::getExtension使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\web\UploadedFile
的用法示例。
在下文中一共展示了UploadedFile::getExtension方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: saveData
/**
* @param \yii\web\UploadedFile $file
*
* @return int
*/
public function saveData(\yii\web\UploadedFile $file)
{
$ext = $file->getExtension();
$baseName = $file->getBaseName();
$model = new \metalguardian\fileProcessor\models\File();
$model->extension = $ext;
$model->base_name = $baseName;
$model->save(false);
return $model->id;
}
示例2: uploadFromPost
/**
*
* @param UploadedFile $uploadedFile
*/
public function uploadFromPost($uploadedFile)
{
if ($uploadedFile === null) {
return;
}
$this->name = ltrim(pathinfo(' ' . $uploadedFile->name, PATHINFO_FILENAME));
$this->extension = $uploadedFile->getExtension();
$this->size = $uploadedFile->size;
$this->type = $uploadedFile->type;
$this->hash = $this->getHashName();
$this->uploadFile($uploadedFile->tempName, $this->hash . "." . $this->extension);
return $this;
}
示例3: beforeValidate
/**
* @inheritdoc
*/
public function beforeValidate()
{
if (!parent::beforeValidate()) {
return false;
}
if ($this->image) {
$content = file_get_contents($this->image->tempName);
$extension = $this->image->getExtension();
if (!($preview = Yii::$app->preview->save($content, $extension))) {
return false;
}
$this->preview = $preview;
}
return true;
}
示例4: getNewFileName
public function getNewFileName()
{
if (!$this->_newFileName) {
$this->_newFileName = Yii::$app->security->generateRandomString() . '.' . $this->_uploadFile->getExtension();
}
return $this->_newFileName;
}
示例5: getSaveFileName
private function getSaveFileName()
{
if (is_callable($this->format) || is_array($this->format)) {
return call_user_func($this->format, $this);
}
//替换日期事件
$t = time();
$d = explode('-', date("Y-y-m-d-H-i-s"));
$format = $this->format;
$format = str_replace("{yyyy}", $d[0], $format);
$format = str_replace("{yy}", $d[1], $format);
$format = str_replace("{mm}", $d[2], $format);
$format = str_replace("{dd}", $d[3], $format);
$format = str_replace("{hh}", $d[4], $format);
$format = str_replace("{ii}", $d[5], $format);
$format = str_replace("{ss}", $d[6], $format);
$format = str_replace("{time}", $t, $format);
$srcName = mb_substr($this->uploadFileInstance->name, 0, mb_strpos($this->uploadFileInstance->name, '.'));
$srcName = preg_replace("/[\\|\\?\"\\<\\>\\/\\*\\\\]+/", '', $srcName);
$format = str_replace("{filename}", $srcName, $format);
//替换随机字符串
$randNum = rand(1, 10000000000) . rand(1, 10000000000);
$matches = [];
if (preg_match("/\\{rand\\:([\\d]*)\\}/i", $format, $matches)) {
$randNumLength = substr($randNum, 0, $matches[1]);
$format = preg_replace("/\\{rand\\:[\\d]*\\}/i", $randNumLength, $format);
}
$ext = $this->uploadFileInstance->getExtension();
return $format . '.' . $ext;
}
示例6: save
/**
* Сохранение файла на сервере
*/
public function save()
{
// Если это действительно экземпляр объекта файла
if ($this->file instanceof UploadedFile) {
if (!FileHelper::createDirectory($this->_path)) {
throw new InvalidParamException("Directory specified in '{$this->_path}' attribute doesn't exist or cannot be created.");
}
$name = FileHelper::getRandomFileName($this->_path, $this->file->getExtension());
$filePath = $this->_path . DIRECTORY_SEPARATOR . $name;
// Если оригинальная картинка сохранилась
if ($this->file->saveAs($filePath)) {
$imageInfo = getimagesize($filePath);
list($width, $height) = $imageInfo;
$this->_width = $width;
$this->_height = $height;
$this->_name = $name;
/** @var ImagePathMap $imagePathMap */
$imagePathMap = Yii::$app->get('imagePathMap', false);
if ($imagePathMap != null) {
$imagePathMap->add($this->_name, $this->_pathID);
}
return true;
}
}
return false;
}
示例7: saveUploadedFile
/**
* Saves uploaded file under the [[tempDirectory]] with random file name
*
* @param UploadedFile $file
* @return string randomly generated file name
* @throws ErrorException when file is not saved
*/
public function saveUploadedFile(UploadedFile $file)
{
do {
$filename = Yii::$app->security->generateRandomString(16) . '.' . $file->getExtension();
$path = $this->getTemporaryPath($filename);
} while (is_file($path));
if (!$file->saveAs($path)) {
throw new ErrorException('Failed to save uploaded file');
}
return $filename;
}
示例8: 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;
}
示例9: uploadPhoto
/**
* Загрузка изображения.
*
* @param UploadedFile $uploadedFile
* @param $userId
* @return bool
*/
public function uploadPhoto(UploadedFile $uploadedFile, $userId)
{
if ($uploadedFile->getBaseName() && !$uploadedFile->getHasError()) {
$photoName = 'profile_photo_' . $userId . '.' . $uploadedFile->getExtension();
$photoSaveFolder = \Yii::getAlias('@app') . $this->profilePhotoFolder . '/' . $userId;
if (!file_exists($photoSaveFolder)) {
mkdir($photoSaveFolder);
}
$photoPath = $photoSaveFolder . '/' . $photoName;
$uploadedFile->saveAs($photoPath);
return $photoName;
}
return false;
}
示例10: afterValidate
/**
* After validation event
*/
public function afterValidate()
{
//Save file to temp.
//Save only new files
//And if file does not have errors after validation
if ($this->saveTemp && $this->file instanceof UploadedFile && !$this->file instanceof FakeUploadedFile && !$this->owner->hasErrors($this->attribute)) {
$fileName = $this->tempPrefix . mt_rand() . '.' . $this->file->getExtension();
$path = $this->generateFilePathInternal($fileName);
FileHelper::createDirectory(dirname($path));
if (copy($this->file->tempName, $path)) {
$this->_tempFile = $path;
$this->owner->{$this->attribute} = $this->generateFileUrlInternal($fileName);
Yii::getLogger()->log('Save temparary file to ' . $path, Logger::LEVEL_INFO);
}
}
}
示例11: runForFile
/**
* @param UploadedFile $uploadedFile
*
* @return bool|string
*/
public function runForFile(UploadedFile $uploadedFile)
{
$extension = $uploadedFile->getExtension();
$baseName = $uploadedFile->getBaseName();
if ($this->useMd5Names) {
$filename = sprintf('%s', md5(time() . $baseName));
} elseif (is_null($this->customName) == false) {
$filename = sprintf("%s", $this->customName);
} else {
$filename = sprintf("%s", $baseName);
}
$filename = $this->getUniqueFilename($filename, $extension);
$filePath = $this->getFilePath($filename);
if ($uploadedFile->saveAs($filePath)) {
return $filename;
}
return false;
}
示例12: saveUploaded
protected function saveUploaded(UploadedFile $file)
{
$dir = $this->getPath(false, true);
$file_name = $file->getBaseName();
$file_ext = $file->getExtension();
$filename = $file_name . '.' . $file_ext;
$index = 0;
while (file_exists($dir . $filename)) {
$index++;
$filename = $file_name . '-' . $index . '.' . $file_ext;
}
FileHelper::createDirectory($dir, 0777, true);
$path = $dir . $filename;
if ($file->saveAs($path)) {
return [$filename, filesize($path)];
}
return false;
}
示例13: setNew
/**
* Sets a new logo image by given temp file
*
* @param CUploadedFile $file
*/
public function setNew(UploadedFile $file)
{
$this->delete();
move_uploaded_file($file->tempName, $this->getPath());
ImageConverter::Resize($this->getPath(), $this->getPath(), array('height' => $this->height, 'width' => 0, 'mode' => 'max', 'transparent' => $file->getExtension() == 'png' && ImageConverter::checkTransparent($this->getPath())));
}
示例14: uploadByUploadFile
/**
* upload by UploadedFile instance.
*
* @param UploadedFile $uploadedFile Uploaded file instance
* @param string $dir uploading file directory.
* @param string $savePath upload file save path. If null, call [[getSavePath()]] to generate one.
* @return File
*/
public function uploadByUploadFile($uploadedFile, $dir, $savePath = null)
{
$file = new File();
if ($uploadedFile === null) {
$file->error = File::UPLOAD_ERROR_NO_UPLOADED_FILE;
return $file;
}
// 检查上传文件是否有错
if ($uploadedFile->getHasError()) {
$file->error = $uploadedFile->error;
return $file;
}
if ($savePath !== null) {
$dir = $this->getDir($savePath);
}
$type = $uploadedFile->getExtension();
$file->error = $this->getErrors($uploadedFile->size, $dir, $type);
if ($file->error !== UPLOAD_ERR_OK) {
return $file;
}
if ($savePath === null) {
$savePath = $this->getSavePath($dir, $type);
}
if ($uploadedFile->saveAs($savePath)) {
$file->url = $this->savePath2Url($savePath);
} else {
$file->error = File::UPLOAD_ERROR_UPLOAD;
}
return $file;
}
示例15: getFileName
/**
* @param UploadedFile $file
* @param callable $formatFileName
* @return string
*/
protected function getFileName(UploadedFile $file, \Closure $formatFileName = null)
{
$filename = $file->name;
if (!is_null($formatFileName)) {
$filename = call_user_func($formatFileName, $file->getBaseName(), $file->getExtension());
}
return $filename;
}