public function testValidate() { $file = new File(); $this->assertFalse($file->validate()); $this->assertArrayHasKey('name', $file->errors); $this->assertArrayHasKey('model', $file->errors); $this->assertArrayHasKey('itemId', $file->errors); $this->assertArrayHasKey('hash', $file->errors); $this->assertArrayHasKey('size', $file->errors); $this->assertArrayHasKey('type', $file->errors); $this->assertArrayHasKey('mime', $file->errors); }
public function detachFile($id) { $file = File::findOne(['id' => $id]); $filePath = $this->getFilesDirPath($file->hash) . DIRECTORY_SEPARATOR . $file->hash . '.' . $file->type; unlink($filePath); $file->delete(); }
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}"); }
public function detachFile($id) { /** @var File $file */ $file = File::findOne(['id' => $id]); if (empty($file)) { return false; } $filePath = $this->getFilesDirPath($file->hash) . DIRECTORY_SEPARATOR . $file->hash . '.' . $file->type; // this is the important part of the override. // the original methods doesn't check for file_exists to be return file_exists($filePath) ? unlink($filePath) && $file->delete() : $file->delete(); }
/** * @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(); }