luya\helpers\FileHelper::getFileContent PHP Method

getFileContent() public static method

Basic helper to retreive the content of a file and catched exception. The filename will auto alias encode by Yii::getAlias function.
Since: 1.0.0-beta7
public static getFileContent ( string $fileName ) : string | boolean
$fileName string The path to the file to get the content
return string | boolean
    public static function getFileContent($fileName)
    {
        try {
            return file_get_contents(Yii::getAlias($fileName));
        } catch (Exception $error) {
            return false;
        }
    }

Usage Example

 public function actionExportDownload($key)
 {
     $sessionkey = Yii::$app->session->get('tempNgRestKey');
     $fileName = Yii::$app->session->get('tempNgRestFileName');
     if ($sessionkey !== base64_decode($key)) {
         throw new Exception('Invalid Export download key.');
     }
     $content = FileHelper::getFileContent('@runtime/' . $sessionkey . '.tmp');
     Yii::$app->session->remove('tempNgRestKey');
     Yii::$app->session->remove('tempNgRestFileName');
     @unlink(Yii::getAlias('@runtime/' . $sessionkey . '.tmp'));
     return Yii::$app->response->sendContentAsFile($content, $fileName . '-export-' . date("Y-m-d-H-i") . '.csv', ['mimeType' => 'application/csv']);
 }