Yii2: Find file by file name




    When the actionDownloadFile() function is invoked, it will look for files in the ImportForm\EXAMPLE_DIR directory that have the .filter file extension. If the file path includes the .filter file extension, the path will be returned; otherwise, false will be returned.

    public function actionDownloadFile($type)
      $filePath = FileHelper::findFiles(ImportForm::EXAMPLE_DIR, ['filter' => function($path) use ($type) {
        if (strpos($path, $type)) {
          return $path;
        return false;
      return Yii::$app->response->sendFile(...$filePath);
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.