显示上传的图片 yii

问题描述:

我是 php 和 yii 的新手.我的表单中有一个文件字段可以上传图像.我使用以下代码使用文件字段上传图像.

I am new to php and yii. I have a file field in my form to upload images. I use the following code to upload the image using file field.

视图中的代码:

<?php echo $form->fileField($model,'logo', array('class'=>'input-file')); ?>
    <img src="<?php 
        echo Yii::app()->request->baseUrl.'/protected/uploads/sitelogo/'.$savedvalues['varLogo']; 
    ?>" width="50" height="50" />
    <?php echo $form->hiddenField($model,'hiddenfile',
            array('value'=>$savedvalues['varLogo'])); ?> 

控制器中的代码:

$randnum = rand(0,100);
$home->varLogo = $randnum.$model->logo;
$file= Yii::app()->getBasePath().'/uploads/sitelogo/'.$randnum.$model->logo;
$model->logo->saveAs($file);    

图片现在上传正常.我已将上传的图像保存在 protected\uploads\ 文件夹中.我试图在编辑图像部分显示上传的图像.但图像不显示.它显示 failed to load the given url in firebug.

The images are uploading fine now. I have saved the uploaded images in protected\uploads\ folder. I am trying to show the uploaded image in edit image section. But the image doesn't display. It Shows failed to load the given url in firebug.

我该如何解决这个问题?

How can i correct this issue?

protected 文件夹由于某种原因受到保护 - 没有人可以访问它

protected folder is protected for some reason - nobody can access it

如果您想访问您的图片,请将它们移动到 public 文件夹(或 public/uploaded)

if you want to access your pictures move them to public folder (or public/uploaded)

这里是 protected

deny from all

当然你可以把你的文件放到受保护的文件夹中(或者让 uploaded 文件夹也不能访问)如果你想为你的用户设置一些 ACL,然后通过你的脚本检查访问、读取和输出文件(类)

of course you can put your files into protected folder(or make uploaded folder not accessible too) if you want some ACL for your users, and then check access, read and output file by your script(class)

在视图中:

<img src="<?php 
    echo Yii::app()->request->baseUrl.'/image.php?url=protected/uploads/sitelogo/'.$savedvalues['varLogo']; 
?>" width="50" height="50" />

在您的控制器或类中:

if (Yii::app()->session['user_can_access_files']) {
    header('Content-Type: image/jpeg');
    readfile($_GET['url']);
} else {
    Yii::app()->user->loginRequired();
}