public function detachFile($id) { $file = File::findOne(['id' => $id]); $filePath = $this->getFilesDirPath($file->hash) . DIRECTORY_SEPARATOR . $file->hash . '.' . $file->type; unlink($filePath); $file->delete(); }
/** * TODO закешировать * @return \yii\db\ActiveQuery */ public function getAttachedFiles() { return $this->hasMany(File::className(), ['itemId' => 'id'])->andWhere(['model' => \yii\helpers\StringHelper::basename(get_class($this))]); }
/** * @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}"); }