显示上传的图片 yii

编程入门 行业动态 更新时间:2024-10-20 07:59:08
本文介绍了显示上传的图片 yii的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我是 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();
}

这篇关于显示上传的图片 yii的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 18:50:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047613.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传   图片   yii

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!