Php 如何在yii2中显示公共_html之外的图像?

Php 如何在yii2中显示公共_html之外的图像?,php,yii,yii2,Php,Yii,Yii2,我的问题是,我试图显示一个位于项目的public_html文件夹之外的图像,为此,在视图中,我在Yii中调用一个控制器方法,运行以下命令: public function actionImagenreadfile2($file) { if(file_exists($file)){ header('Content-Type: '. FileHelper::getMimeType($file)); ob_clean(); readfile($

我的问题是,我试图显示一个位于项目的public_html文件夹之外的图像,为此,在视图中,我在Yii中调用一个控制器方法,运行以下命令:

public function actionImagenreadfile2($file) {

    if(file_exists($file)){
        header('Content-Type: '. FileHelper::getMimeType($file));
        ob_clean();
        readfile($file);
    }
}
我从视图中调用此控制器的方式如下:

<img src="<?php Yii::$app->runAction('ficha/imagenreadfile2', ['file' => $imagen]); ?>">
runAction('ficha/imagenredfile2',['file'=>$imagen]);?>">

其中$imagen是图像路径,但在显示图像显示为文本/平面时。

您应该修复视图:

<img src="<?= Url::to(['ficha/imagenreadfile2', 'file' => $imagen]); ?>">
阅读更多关于


最重要的是,你应该检查
$file
,因为你的行为是完全不安全的…

你可以参考这篇文章:这是类似的需要检查$file如何?
return \Yii::$app->response->sendFile($file);