PHP Class yii\web\ UploadedFile Code Examples

PHP yii\web\UploadedFile - 30 examples found. These are the top rated real world PHP examples of class yii\web\UploadedFile extracted from open source projects. You can rate examples to help us improve the quality of examples
You can call UploadedFile::getInstance to retrieve the instance of an uploaded file, and then use UploadedFile::saveAs to save it on the server. You may also query other information about the file, including [[name]], [[tempName]], [[type]], [[size]] and [[error]]. For more details and usage information on UploadedFile, see the guide article on handling uploads.
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends yii\base\Object
Example #1
0
 /**
  * @param Book|null $book
  * @return boolean whether the book successfully edited
  */
 public function update($book)
 {
     if ($book) {
         if ($this->validate()) {
             $book->name = $this->name;
             $book->author_id = $this->author_id;
             $book->date = $this->date;
             if ($this->imageFile || $this->deleteImage) {
                 if (!empty($book->preview)) {
                     $imageToDelete = Yii::getAlias('@webroot') . Book::IMAGE_FOLDER . $book->preview;
                     if (file_exists($imageToDelete)) {
                         unlink($imageToDelete);
                     }
                     $book->preview = '';
                 }
             }
             if ($this->imageFile) {
                 $imageName = Yii::$app->security->generateRandomString() . '.' . $this->imageFile->extension;
                 if (!$this->imageFile->saveAs(Yii::getAlias('@webroot') . Book::IMAGE_FOLDER . $imageName)) {
                     return false;
                 }
                 $book->preview = $imageName;
             }
             return $book->save();
         }
     }
     return false;
 }
 /**
  * Saves a file
  * @param \yii\web\UploadedFile $file the file uploaded
  * @param string $name the name of the file. If empty, it will be set to the name of the uploaded file
  * @param array $options to save the file. The options can be any of the following:
  *  - `folder` : whether we should create a subfolder where to save the file
  * @return boolean
  */
 public function save($file, $name, $options = [])
 {
     $folder = ArrayHelper::getValue($options, 'folder');
     $path = $folder ? $this->getBasePath() . DIRECTORY_SEPARATOR . $folder . ltrim($name, DIRECTORY_SEPARATOR) : $this->getBasePath() . DIRECTORY_SEPARATOR . ltrim($name, DIRECTORY_SEPARATOR);
     @mkdir(dirname($path), 0777, true);
     return $file->saveAs($path);
 }
Example #3
0
 /**
  * copy Yii2 UploadedFile
  * @param UploadedFile $upoadFile 
  * @return boolean
  * @throws NotFoundHttpException
  */
 public function uploadYii2UloadFile(UploadedFile $upoadFile)
 {
     if (!$upoadFile->saveAs($this->getFilePath())) {
         throw new NotFoundHttpException(Yii::t('d3files', 'The uploaded file does not exist.'));
     }
     return true;
 }
Example #4
0
 /**
  * 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;
 }
Example #5
0
 /**
  * @inherited
  */
 public function beforeSave($insert)
 {
     if ($this->file && $this->file instanceof UploadedFile && parent::beforeSave($insert)) {
         FileHelper::createDirectory(dirname($this->filename));
         return $this->file->saveAs($this->filename, false);
     }
     return false;
 }
Example #6
0
 public function afterSave($insert, $changedAttributes)
 {
     $this->preview_input = UploadedFile::getInstance($this, 'preview_input');
     if (!empty($this->preview_input)) {
         $this->preview_input->saveAs($this->preview_picture);
     }
     parent::afterSave($insert, $changedAttributes);
 }
Example #7
0
 public function upload()
 {
     if ($this->validate()) {
         $this->filename = \Yii::$app->security->generateRandomString() . '.' . $this->file->extension;
         $this->file->saveAs("uploads/{$this->filename}");
         return true;
     }
     return false;
 }
 public function upload()
 {
     $done = $this->videoFile->saveAs('uploads/' . $this->videoFile->baseName . '.' . $this->videoFile->extension);
     if ($done) {
         return true;
     } else {
         return false;
     }
 }
Example #9
0
 public static function uploadLogo(UploadedFile $fileInstance, $dir = '', $resizeWidth = null, $resizeHeight = null, $resizeCrop = false)
 {
     $fileName = Upload::getUploadPath($dir) . DIRECTORY_SEPARATOR . 'logo.' . $fileInstance->extension;
     $uploaded = $resizeWidth ? self::copyResizedImage($fileInstance->tempName, $fileName, $resizeWidth, $resizeHeight, $resizeCrop) : $fileInstance->saveAs($fileName);
     if (!$uploaded) {
         throw new HttpException(500, 'Cannot upload file "' . $fileName . '". Please check write permissions.');
     }
     return Upload::getLink($fileName);
 }
Example #10
0
 public function upload()
 {
     if ($this->validate()) {
         $this->csvFile->saveAs(Yii::getAlias('@uploads') . '/' . $this->csvFile->baseName . '.' . $this->csvFile->extension);
         return true;
     } else {
         return false;
     }
 }
 public function upload()
 {
     if ($this->validate()) {
         $this->excelFile->saveAs(Yii::$app->basePath . '/uploads/' . $this->excelFile->baseName . '.' . $this->excelFile->extension);
         return true;
     } else {
         return false;
     }
 }
Example #12
0
 public function upload()
 {
     if ($this->validate()) {
         $this->IMAGE->saveAs('upload/' . $this->IMAGE->baseName . '.' . $this->IMAGE->extension);
         return true;
     } else {
         return false;
     }
 }
Example #13
0
 public function upload()
 {
     if ($this->validate()) {
         $this->imageFile->saveAs('img/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
         return true;
     } else {
         return false;
     }
 }
Example #14
0
 public function afterSave($insert, $changedAttributes)
 {
     if ($this->scenario == self::SCENARIO_FILE_UPLOAD) {
         $fileName = $this->getFileName();
         FileHelper::createDirectory(dirname($fileName));
         $this->fileUpload->saveAs($fileName);
     }
     parent::afterSave($insert, $changedAttributes);
 }
Example #15
0
 public function uploadImage()
 {
     if ($this->validate() && $this->imageFile) {
         $this->imageFile->saveAs(Yii::getAlias("@webroot") . '/uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
         return true;
     } else {
         return false;
     }
 }
Example #16
0
 public function upload()
 {
     if ($this->imageFile = UploadedFile::getInstance($this, 'imageFile')) {
         $this->preview = md5($this->imageFile->baseName . time()) . '.' . $this->imageFile->extension;
         $this->imageFile->saveAs(Yii::getAlias('@webroot') . '/' . self::UPLOAD_DIR . $this->preview);
         $this->imageFile = NULL;
         Image::thumbnail('@webroot/' . self::UPLOAD_DIR . $this->preview, 120, 120)->save(Yii::getAlias('@webroot') . '/' . self::UPLOAD_DIR . 'thumb/' . $this->preview, ['quality' => 80]);
     }
 }
Example #17
0
 public function store(UploadedFile $file)
 {
     $storePath = \Yii::getAlias('@app/data');
     if ($file->saveAs($storePath . '/' . $file->name, true)) {
         $this->fileAttributes['store'] = 'data/' . $file->name;
         $this->fileAttributes['url'] = \Yii::$app->urlManager->hostInfo . '/' . $this->fileAttributes['store'];
         return true;
     }
     return false;
 }
Example #18
0
 public function upload()
 {
     if ($this->validate()) {
         //yii::setAlias('@path1', 'localhost/basic/');
         $this->imageFile->saveAs(\Yii::$app->basePath . '/uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
         return true;
     } else {
         return false;
     }
 }
 /**
  * Saving file to temporary directory and resize it if needed.
  * @param UploadedFile $file File needs to save.
  * @param integer $maxImageWidth Maximum image width.
  * @param integer $maxImageHeight Maximum image height.
  * @param integer $quality Image quality (for jpeg only).
  * @return string Name of file relative to web root.
  */
 public static function save(UploadedFile $file, $maxImageWidth, $maxImageHeight, $quality)
 {
     $name = self::generateName($file->name);
     $filename = Yii::getAlias('@webroot') . $name;
     $file->saveAs($filename);
     $image = new ImageFile($filename, ['jpegQuality' => $quality]);
     $image->bounds($maxImageWidth, $maxImageHeight);
     $image->save();
     return $name;
 }
Example #20
0
 public function upload()
 {
     $file_path = \Yii::getAlias('@upload');
     if ($this->validate()) {
         $this->file->saveAs($file_path . $this->file->baseName . '.' . $this->file->extension);
         return true;
     } else {
         return false;
     }
 }
 /**
  * @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;
 }
Example #22
0
 /**
  * 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;
 }
Example #23
0
 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;
     }
 }
Example #24
0
 /**
  * @param UploadedFile $file
  */
 public function saveFile(UploadedFile $file)
 {
     if ($file) {
         $folder = \Yii::getAlias('@webroot') . '/uploads/settings/' . $this->key . '/';
         if (is_dir($folder) == false) {
             mkdir($folder, 0755, true);
         }
         $file->saveAs($folder . $file->name);
         Image::thumbnail($folder . $file->name, 160, 90)->save($folder . 'thumb_' . $file->name, ['quality' => 90]);
     }
 }
Example #25
0
 public function upload()
 {
     if ($this->validate()) {
         $this->imageHash = Yii::$app->security->generateRandomString(20) . '.' . $this->imageFile->extension;
         $path = 'uploads/' . $this->imageHash;
         $this->imageFile->saveAs($path);
         return true;
     } else {
         return false;
     }
 }
Example #26
0
 /**
  * @return null|string
  */
 public function upload()
 {
     if ($this->validate()) {
         $fileName = $this->imageFile->baseName . '.' . $this->imageFile->extension;
         $filePath = __DIR__ . '../../web/files/' . $fileName;
         $this->imageFile->saveAs($filePath);
         return $filePath;
     } else {
         return null;
     }
 }
Example #27
0
 public function upload()
 {
     $uid = Yii::$app->user->getId();
     if ($this->validate()) {
         $path = 'uploads/excels/' . $uid . '_' . time() . '.' . $this->excelFile->extension;
         $this->excelFile->saveAs($path);
         return $path;
     } else {
         return false;
     }
 }
Example #28
0
 /**
  * @return bool
  */
 public function upload()
 {
     if ($this->validate()) {
         if (!file_exists('uploadImage')) {
             mkdir('uploadImage', 0777);
         }
         $this->imageFile->saveAs('uploadImage/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
         return true;
     } else {
         return false;
     }
 }
Example #29
0
 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;
 }
Example #30
0
 public function upload($id)
 {
     if ($this->validate()) {
         $rootpath = 'upload/photo/' . $id . '/';
         $path = $rootpath . $this->photo->baseName . '.' . $this->photo->extension;
         @mkdir($rootpath, true);
         $this->photo->saveAs($path);
         return $path;
     } else {
         return false;
     }
 }