/** * @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; }
/** * * @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; }
/** * @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; }
public function getNewFileName() { if (!$this->_newFileName) { $this->_newFileName = Yii::$app->security->generateRandomString() . '.' . $this->_uploadFile->getExtension(); } return $this->_newFileName; }
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; }
/** * Сохранение файла на сервере */ 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; }
/** * 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; }
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; }
/** * Загрузка изображения. * * @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; }
/** * 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); } } }
/** * @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; }
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; }
/** * 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()))); }
/** * 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; }
/** * @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; }
/** * @param UploadedFile $uploadedFile * @return string */ private function getFullName($uploadedFile) { if ($this->fullName) { return $this->fullName; } //替换日期事件 $t = time(); $d = explode('-', date("Y-y-m-d-H-i-s")); $format = $this->imagePathFormat; $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); //替换随机字符串 $randNum = rand(1, 10000000000) . rand(1, 10000000000); if (preg_match("/\\{rand\\:([\\d]*)\\}/i", $format, $matches)) { $format = preg_replace("/\\{rand\\:[\\d]*\\}/i", substr($randNum, 0, $matches[1]), $format); } $ext = $uploadedFile->getExtension(); $this->fullName = $format . '.' . $ext; return $this->fullName; }
protected function saveUploaded(UploadedFile $image) { $dir = $this->getPath(false, false, true); $image_name = $image->getBaseName(); $image_ext = $image->getExtension(); $filename = $image_name . '.' . $image_ext; $index = 0; while (file_exists($dir . $filename)) { $index++; $filename = $image_name . '-' . $index . '.' . $image_ext; } $path = $dir . $filename; $this->createFileDir($path); if ($image->saveAs($path)) { return $filename; } return false; }
protected function generateFileName(UploadedFile $file) { return uniqid() . '.' . $file->getExtension(); }
/** * save uploaded file * * @param strind|\yii\web\UploadedFile $file */ protected function saveFile($event, $file, $attribute) { if ($file instanceof \yii\web\UploadedFile) { $fileObject = new \nvlad\storage\objects\Image($file->tempName); $fileObject->file = '0000/' . uniqid() . '.' . $file->getExtension(); if ($this->_storage->save($fileObject)) { $event->sender->setAttribute($attribute, $this->_storage->getUrl($fileObject)); } } }
/** * Create new unique file name * * @param UploadedFile $uploadedFile * @param int $prefix * * @return string */ protected function createFileName($uploadedFile, $prefix = 1) { $_fileName = time() . $prefix . '.' . $uploadedFile->getExtension(); $uploadedFile->name = $this->path . $_fileName; \yii\helpers\FileHelper::createDirectory($this->path, 0775, true); return file_exists($uploadedFile->name) ? $this->createFileName($uploadedFile, $prefix + 1) : $_fileName; }