Exemple #1
0
 public function detachFile($id)
 {
     $file = File::findOne(['id' => $id]);
     $filePath = $this->getFilesDirPath($file->hash) . DIRECTORY_SEPARATOR . $file->hash . '.' . $file->type;
     unlink($filePath);
     $file->delete();
 }
Exemple #2
0
 /**
  * TODO закешировать
  * @return \yii\db\ActiveQuery
  */
 public function getAttachedFiles()
 {
     return $this->hasMany(File::className(), ['itemId' => 'id'])->andWhere(['model' => \yii\helpers\StringHelper::basename(get_class($this))]);
 }
Exemple #3
0
 /**
  * @return File[]
  * @throws \Exception
  */
 public function getFiles()
 {
     $fileQuery = File::find()->where(['itemId' => $this->owner->id, 'model' => $this->getModule()->getShortClass($this->owner)]);
     $fileQuery->orderBy(['id' => SORT_ASC]);
     return $fileQuery->all();
 }
 public function actionDownload($id)
 {
     $file = File::findOne(['id' => $id]);
     $filePath = $this->getModule()->getFilesDirPath($file->hash) . DIRECTORY_SEPARATOR . $file->hash . '.' . $file->type;
     return \Yii::$app->response->sendFile($filePath, "{$file->name}.{$file->type}");
 }