saveAs() public method

Note that this method uses php's move_uploaded_file() method. If the target file $file already exists, it will be overwritten.
See also: error
public saveAs ( string $file, boolean $deleteTempFile = true ) : boolean
$file string the file path used to save the uploaded file
$deleteTempFile boolean whether to delete the temporary file after saving. If true, you will not be able to save the uploaded file again in the current request.
return boolean true whether the file is saved successfully
Beispiel #1
5
 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);
 }
 public function upload()
 {
     if ($this->validate()) {
         $this->IMAGE->saveAs('upload/' . $this->IMAGE->baseName . '.' . $this->IMAGE->extension);
         return true;
     } else {
         return false;
     }
 }
Beispiel #3
3
 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;
     }
 }
 public function upload()
 {
     if ($this->validate()) {
         $this->excelFile->saveAs(Yii::$app->basePath . '/uploads/' . $this->excelFile->baseName . '.' . $this->excelFile->extension);
         return true;
     } else {
         return false;
     }
 }
Beispiel #5
2
 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);
 }
Beispiel #6
2
 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;
     }
 }
Beispiel #7
1
 /**
  * @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;
 }
Beispiel #8
1
 public function upload()
 {
     if ($this->validate()) {
         $this->imageFile->saveAs('img/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
         return true;
     } else {
         return false;
     }
 }
Beispiel #9
1
 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;
 }
Beispiel #10
1
 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;
     }
 }
Beispiel #11
1
 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;
     }
 }
Beispiel #12
1
 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;
     }
 }
Beispiel #13
1
 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;
     }
 }
Beispiel #14
1
 /**
  * @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;
     }
 }
Beispiel #15
0
 /**
  * @return bool
  */
 public function upload()
 {
     if ($this->validate()) {
         return $this->file->saveAs($this->getPath(), true);
     }
     return false;
 }
Beispiel #16
0
 public function upload()
 {
     if ($this->validate()) {
         return $this->file->saveAs(Yii::$app->controller->module->getFilePath($this->getFileName()), true);
     }
     return false;
 }
Beispiel #17
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;
 }
Beispiel #18
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]);
     }
 }
 public function upload()
 {
     $done = $this->videoFile->saveAs('uploads/' . $this->videoFile->baseName . '.' . $this->videoFile->extension);
     if ($done) {
         return true;
     } else {
         return false;
     }
 }
Beispiel #20
0
 public function save()
 {
     $this->filename = $this->uploadedFile->baseName . '.' . $this->uploadedFile->extension;
     if ($this->uploadedFile->saveAs(Yii::getAlias('@webroot' . $this->uploadPath) . $this->filename)) {
         $this->model->filename = $this->filename;
         $this->model->path = $this->uploadPath;
         return true;
     }
     return false;
 }
Beispiel #21
0
 public function Upload()
 {
     /** @var Imagable $image */
     $image = Yii::$app->shop_imagable;
     // save original
     $this->_orig_image_name = $this->imageFile->baseName . $this->_image_extension;
     $this->imageFile->saveAs($image->imagesPath . $this->_orig_image_name);
     // create small, thumb & big
     $this->_image_name = $image->create($this->_category, $image->imagesPath . $this->_orig_image_name);
     // delete original
     unlink($image->imagesPath . $this->_orig_image_name);
 }
Beispiel #22
0
 public function upload()
 {
     if ($this->validate()) {
         $this->image = 'uploads/news/' . $this->imageFile->baseName . '.' . $this->imageFile->extension;
         $this->imageFile->saveAs(Yii::getAlias('@webroot') . '/' . $this->image);
         $this->image = Yii::getAlias('@web') . '/' . $this->image;
         $this->imageFile = NULL;
         return true;
     } else {
         return false;
     }
 }
Beispiel #23
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;
     }
 }
Beispiel #24
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;
     }
 }
Beispiel #25
0
 public function uploadFile(UploadedFile $fileUpload)
 {
     if ($this->name) {
         @unlink($this->getFileName());
     }
     $this->original_name = $fileUpload->name;
     $this->file_size = filesize($fileUpload->tempName);
     $this->name = $this->generateName();
     $this->updateAttributes(['original_name', 'name', 'file_size']);
     $fileName = $this->getFileName();
     FileHelper::createDirectory(dirname($fileName));
     $this->fileUpload->saveAs($fileName);
 }
Beispiel #26
0
 /**
  * Upload file
  * @param File $model
  * @return bool
  */
 public function upload($model)
 {
     if ($this->validate()) {
         $model->name = FileHelper::removeExtension($this->file->name);
         $model->real_name = uniqid();
         $model->ext = $this->file->extension;
         $model->type = $this->file->type;
         $this->file->saveAs(FileHelper::getFilePath($model));
         return $model;
     } else {
         return false;
     }
 }
Beispiel #27
0
 public function upload1($arenda_type_id)
 {
     if (!empty($this->imageFile)) {
         if ($this->validate()) {
             if (!file_exists("../web/images/arenda_types_images/" . $arenda_type_id)) {
                 mkdir("../web/images/arenda_types_images/" . $arenda_type_id, 0777);
             }
             $image_name = md5($this->imageFile->baseName) . date('His') . '.' . $this->imageFile->extension;
             $this->imageFile->saveAs('images/arenda_types_images/' . $arenda_type_id . '/' . $image_name);
             $model = ImagesForArendaTypes::findOne(['arenda_type_id' => $arenda_type_id]);
             if (!empty($model)) {
                 if (file_exists("../web/images/arenda_types_images/" . $arenda_type_id . '/' . $model->url)) {
                     unlink("../web/images/arenda_types_images/" . $arenda_type_id . '/' . $model->url);
                 }
             }
             if (empty($model)) {
                 $model = new ImagesForArendaTypes();
             }
             $model->url = $image_name;
             $model->arenda_type_id = $arenda_type_id;
             $model->save();
             return true;
         } else {
             return false;
         }
     }
     if (!empty($this->imageFile1)) {
         if ($this->validate()) {
             if (!file_exists("../web/images/arenda_types_images/" . $arenda_type_id)) {
                 mkdir("../web/images/arenda_types_images/" . $arenda_type_id, 0777);
             }
             $image_name = md5($this->imageFile1->baseName) . date('His') . '.' . $this->imageFile1->extension;
             $this->imageFile1->saveAs('images/arenda_types_images/' . $arenda_type_id . '/' . $image_name);
             $model = ImagesForArendaTypes::findOne(['arenda_type_id' => $arenda_type_id]);
             if (!empty($model)) {
                 if (file_exists("../web/images/arenda_types_images/" . $arenda_type_id . '/' . $model->url_black)) {
                     unlink("../web/images/arenda_types_images/" . $arenda_type_id . '/' . $model->url_black);
                 }
             }
             if (empty($model)) {
                 $model = new ImagesForArendaTypes();
             }
             $model->url_black = $image_name;
             $model->arenda_type_id = $arenda_type_id;
             $model->save();
             return true;
         } else {
             return false;
         }
     }
 }
Beispiel #28
0
 public function upload()
 {
     if (!$this->hasErrors()) {
         $this->thumbnail = UploadedFile::getInstance($this, 'thumbnail');
         if ($this->thumbnail == null) {
             return false;
         }
         $this->thumbnail->saveAs('images/' . $this->thumbnail->baseName . '.' . $this->thumbnail->extension);
         $this->thumbnail = $this->thumbnail->baseName . '.' . $this->thumbnail->extension;
         return true;
     } else {
         return false;
     }
 }
 public function upload()
 {
     if ($this->validate() && $this->imageFile) {
         $uploadDir = ArrayHelper::getValue(\Yii::$app->params, 'upload.common') . date('Ymd') . '/';
         if (!FileHelper::createDirectory($uploadDir)) {
             $this->addError('文件夹创建失败!请检查权限!', 'imageFile');
             return false;
         }
         $this->savePath = $uploadDir . \Yii::$app->getSecurity()->generateRandomString() . '.' . $this->imageFile->extension;
         return $this->imageFile->saveAs($this->savePath);
     } else {
         return false;
     }
 }
Beispiel #30
-1
 public function upload()
 {
     if ($this->validate()) {
         $this->csvFile->saveAs(Yii::getAlias('@uploads') . '/' . $this->csvFile->baseName . '.' . $this->csvFile->extension);
         return true;
     } else {
         return false;
     }
 }