Yii 显示受保护文件夹中的图像

Yii 显示受保护文件夹中的图像,yii,imageview,Yii,Imageview,我在Yii中有一个受保护的文件夹,我希望在站点中显示其中的一些图像。我尝试了以下代码,它在站点/索引控制器中工作,因为它只返回我想要的图像 然而,当我试图分离代码时,它没有工作。感谢您的帮助 型号 public function getImage() // will take file identifier as @param { $imageID = '2562584569'; // will eventually be dynamically assigned

我在Yii中有一个受保护的文件夹,我希望在站点中显示其中的一些图像。我尝试了以下代码,它在站点/索引控制器中工作,因为它只返回我想要的图像

然而,当我试图分离代码时,它没有工作。感谢您的帮助

型号

    public function getImage() // will take file identifier as @param       
{

    $imageID = '2562584569'; // will eventually be dynamically assigned

    $image = Images::model()->find('tmp_name=:id', array('id' => $imageID)); 

    $dest = Yii::getPathOfAlias('application.uploads');

    $file = $dest .'/' . $image->tmp_name . '.' . $image->extension;

    if(file_exists($file)){
    ob_clean();
    header('Content-Type:' . $image->logo_type);
    readfile($file);
    exit;
    }

}
public function actionImage($data)       
{

$image = Images::model()->find('tmp_name=:data', array('id' => $data)); 

$dest = Yii::getPathOfAlias('application.uploads');

$file = $dest .'/' . $image->tmp_name . '.' . $image->extension;

if(file_exists($file)){
ob_clean();
header('Content-Type:' . $image->logo_type);
readfile($file);
exit;
}

}
并且在视图中

CHtml::link('<img src="' . Yii::app()->request->baseUrl .'/images/image" />', array('product/index', 'id'=>$data['product_id'], 'slug'=> $data['product_slug']));
CHtml::link('request->baseUrl./images/image/>',array('product/index','id'=>$data['product\u id'],'slug'=>$data['product\u slug']);
谢谢

Jonny

无法从客户端浏览器访问“受保护”文件夹。这会阻止用户访问重要文件,如您的源代码

如果要将图像存储在“受保护”中并使其可访问,则需要使用
CAssetManager
发布它们

用法类似于:

$path = Yii::app()->basePath.'/path-inside-protected';
$yourImageUrl = Yii::app()->assetManager->publish($path);
Yii然后将该文件用作资产,将其复制到“资产”文件夹,同级为“受保护”。之后,您只需使用HTML上返回的url即可

<img src="<?php echo $yourImageUrl ?>">
“>

我是这样做的

CHtml::link('<img src="' . $this->createUrl('/images/image', array('data'=>$data['data'])) . '" />', array('product/index', 'id'=>$data['product_id'], 'slug'=> $data['product_slug']));

感谢所有帮助

好的,我正在检索结果列表,结果集的一部分是产品徽标,因此我一直使用
控制器/方法/id
作为src,并将
id
传递给方法。因此images/image/2562584569与图像控制器、getimage()方法、文件id有关。尽管事情是这样,但仍然无法获取要显示的图像。。在使用assetManager复制资产目录中的文件之前,您无法查看存储在受保护文件夹中的图像,无论您尝试何种方法。另一种方法是在Apache中工作时修改.htaccess文件。我猜你是。然而,我强烈建议不要修改它。我在考虑这个总体想法,但不确定是否能实现。基本上,src标记调用特定的模型/方法/id,确认文件存在,然后获取一个代理返回以访问文件并返回图像?那行吗?我还没试过。。但我猜。。这可能有用。一定要告诉我你的结果是什么。