首页 > php > 如何使用zend框架在用户帐户页面中显示用户头像/图像?

如何使用zend框架在用户帐户页面中显示用户头像/图像? (How to display the user avatar/image in the user account page using zend framework?)

问题

图像由用户上载并存储在public / users文件夹中。视图脚本如下:

<?php echo $this->render("includes/header.phtml"); ?>
<div id="header">
<div class="w">
    <div id="logo"><a href="/">SyncFan.com</a></div>
    <div id="mainmenu">
        <ul>
            <li class="selected"><a href="/account/accountmanager"><span>My Account</span></a></li>             
            <li onMouseMove="$('.submenu',this).show();$(this).addClass('hover');"  onMouseOut="$('.submenu',this).hide();$(this).removeClass('hover');"><a href="/artist/list"><span>View Artists</span></a></li>
            <li onMouseMove="$('.submenu',this).show();$(this).addClass('hover');"  onMouseOut="$('.submenu',this).hide();$(this).removeClass('hover');"><a href="/account/update"><span>Update Account</span></a></li>
            <li onMouseMove="$('.submenu',this).show();$(this).addClass('hover');"  onMouseOut="$('.submenu',this).hide();$(this).removeClass('hover');">
                <a href="/account/logout"><span>Log Out</span></a>
            </li>
        </ul>
    </div>
</div>
</div>

<div id="content" class="w">
<h2>SyncFan My Page - <?php $this->session = new Zend_Session_Namespace('login');echo $this->escape($this->session->username); ?></h2>
<!--- MAIN CONTAINER -->
<table class="table">
<tr><td valign="top">
<!--PROFILE DATA-->
<table class="table ">
<tr><td>Username: <?php echo $this->session->username; ?></td></tr>
<tr><td><img src="<?php echo '../public/users/'.$this->avatar; ?>"/></td></tr>
<tr><td><?php echo $this->aboutme; ?></td></tr>
<tr><td>Date Joined: <?php echo $this->session->dateJoined?></td></tr>
</table>
</td>
<td valign="top">
<!--FAVORITE ARTIST LIST-->
<table class="table table-hover">
<?php if($this->artists){?>
<tr align="center">
<td><b>Artist Name</b></td>
<td><b>Date Became A Fan</b></td>
</tr>
<?php foreach($this->artists as $artist){?>
<tr>
<td><a href='/artist/video-list?artist=<?php echo
urlencode($artist['artist_name'])?>'>
<?php echo $this->escape($artist['artist_name']); ?></a></td>
<td><?php echo $this->escape($artist['date_became_fan']); ?></td></tr>
<?php }
}else{
?>
<tr><td>You currently have no favorite artists.</td></tr>
<?php }?>
</table>
</td>
</tr>

</table>
</div>
<?php echo $this->render("includes/footer.phtml"); ?>

在帐户视图中。HTML语句的输出

<img src="<?php echo '../public/users/'.$this->avatar; ?>"/> 

是空的。我能够获取上传图像的详细信息,但无法将其显示回用户帐户。

<?php var_dump($this->avatar); ?>

上述语句的输出是字符串'filename.jpg'(长度=文件长度)。

我也检查了.htaccess文件但是找不到任何错误。任何帮助将不胜感激。

解决方法

<img src"<?php echo '/users/'.$this->avatar;?>"/>

浏览器无法访问文档根目录之外的任何内容,可能是/public这样,因此路径应该是相对的。

编辑:所以模板中的行是:

<tr><td><img src="<?php echo '../public/users/'.$this->avatar; ?>"/></td></tr>

如果您查看生成的HTML页面的来源,您应该得到:

<tr><td><img src="../public/users/filename.jpg"/></td></tr>

(这不起作用)。根据我的建议,模板中的行实际上应该是:

<tr><td><img src"<?php echo '/users/'.$this->avatar;?>"/></td></tr>

哪个应该给你:

<tr><td><img src"/users/filename.jpg"/></td></tr>

如果你真的得到:

<tr><td><img src""/></td></tr>

然后你错过了'回声'。如果你得到:

<tr><td><img src"/users/"/></td></tr>

那么$this->avatar实际上并不像你说的那样包含filename.jpg。

如果整个页面都是空白的,那么实际上会生成PHP错误,但您已display_errors关闭(请检查您的Web服务器错误日志)。如果不知道你到底得到了什么,我所能做的就是猜测。

问题

The image is uploaded by the user and stored in public/users folder. The view script is as follows:

<?php echo $this->render("includes/header.phtml"); ?>
<div id="header">
<div class="w">
    <div id="logo"><a href="/">SyncFan.com</a></div>
    <div id="mainmenu">
        <ul>
            <li class="selected"><a href="/account/accountmanager"><span>My Account</span></a></li>             
            <li onMouseMove="$('.submenu',this).show();$(this).addClass('hover');"  onMouseOut="$('.submenu',this).hide();$(this).removeClass('hover');"><a href="/artist/list"><span>View Artists</span></a></li>
            <li onMouseMove="$('.submenu',this).show();$(this).addClass('hover');"  onMouseOut="$('.submenu',this).hide();$(this).removeClass('hover');"><a href="/account/update"><span>Update Account</span></a></li>
            <li onMouseMove="$('.submenu',this).show();$(this).addClass('hover');"  onMouseOut="$('.submenu',this).hide();$(this).removeClass('hover');">
                <a href="/account/logout"><span>Log Out</span></a>
            </li>
        </ul>
    </div>
</div>
</div>

<div id="content" class="w">
<h2>SyncFan My Page - <?php $this->session = new Zend_Session_Namespace('login');echo $this->escape($this->session->username); ?></h2>
<!--- MAIN CONTAINER -->
<table class="table">
<tr><td valign="top">
<!--PROFILE DATA-->
<table class="table ">
<tr><td>Username: <?php echo $this->session->username; ?></td></tr>
<tr><td><img src="<?php echo '../public/users/'.$this->avatar; ?>"/></td></tr>
<tr><td><?php echo $this->aboutme; ?></td></tr>
<tr><td>Date Joined: <?php echo $this->session->dateJoined?></td></tr>
</table>
</td>
<td valign="top">
<!--FAVORITE ARTIST LIST-->
<table class="table table-hover">
<?php if($this->artists){?>
<tr align="center">
<td><b>Artist Name</b></td>
<td><b>Date Became A Fan</b></td>
</tr>
<?php foreach($this->artists as $artist){?>
<tr>
<td><a href='/artist/video-list?artist=<?php echo
urlencode($artist['artist_name'])?>'>
<?php echo $this->escape($artist['artist_name']); ?></a></td>
<td><?php echo $this->escape($artist['date_became_fan']); ?></td></tr>
<?php }
}else{
?>
<tr><td>You currently have no favorite artists.</td></tr>
<?php }?>
</table>
</td>
</tr>

</table>
</div>
<?php echo $this->render("includes/footer.phtml"); ?>

in the account view. The output of the HTML statement

<img src="<?php echo '../public/users/'.$this->avatar; ?>"/> 

is empty. I'm able to get the details of the uploaded image but not able to present it back to the user account.

<?php var_dump($this->avatar); ?>

The ouput of the above statement is string 'filename.jpg' (length=length of file).

I have checked the .htaccess file also but could not find anything wrong in it. Any help would be greatly appreciated.

解决方法

<img src"<?php echo '/users/'.$this->avatar;?>"/>

The browser can't access anything outside your document root, which presumably is /public, so the path should be relative to that.

Edit: So the line from your template is:

<tr><td><img src="<?php echo '../public/users/'.$this->avatar; ?>"/></td></tr>

if you view the source of the resulting HTML page, you should get:

<tr><td><img src="../public/users/filename.jpg"/></td></tr>

(which won't work). With my suggestion above, the line from your template should actually be:

<tr><td><img src"<?php echo '/users/'.$this->avatar;?>"/></td></tr>

which should give you:

<tr><td><img src"/users/filename.jpg"/></td></tr>

If you are actually getting:

<tr><td><img src""/></td></tr>

then you've missed the 'echo'. If you're getting:

<tr><td><img src"/users/"/></td></tr>

then $this->avatar does not in fact contain filename.jpg like you said.

If the entire page is blank, there's actually a PHP error being generated but you have display_errors turned off (check your web server error log). Without knowing exactly what you are getting, all I can do is guess.

相似信息