/** * Finds the File model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return File the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = File::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @param $id * @return $this * @throws \yii\web\NotFoundHttpException */ public function actionAttachmentDownload($id) { $model = File::findOne($id); if (!$model) { throw new NotFoundHttpException(); } return \Yii::$app->response->sendStreamAsFile(\Yii::$app->fileStorage->getFilesystem()->readStream($model->file_index), $model->file_name); }
public function run($id, $hex) { // return "deleted"; if (Yii::$app->request->isGet) { $file = \common\models\File::findOne($id); if ($file->id && $hex == md5(Yii::$app->request->cookieValidationKey . $_SERVER['REMOTE_ADDR'] . $file->filename . $file->id)) { $dir = $_SERVER['DOCUMENT_ROOT'] . '/' . $file->path; if (is_file($dir . '/' . $file->filename)) { unlink($dir . '/' . $file->filename); } $file->delete(); $styles = scandir($dir); foreach ($styles as $style) { if (is_dir($dir . '/' . $style) && is_file($dir . '/' . $style . '/' . $file->filename)) { unlink($dir . '/' . $style . '/' . $file->filename); } } Yii::$app->db->createCommand('UPDATE {{file}} SET delta = delta -1 WHERE nid=:nid AND model=:model AND field=:field AND delta > :delta', [':nid' => $file->nid, ':model' => $file->model, ':field' => $file->field, ':delta' => $file->delta])->execute(); return "deleted"; } return false; } }