0

Hi ive run into what I thought would be a simple obstacle however, its become quite a hassle I want to display a picture on my website however it will be different for every user that is logged in. I tried this

<img src="<?php echo 'screenshots/img_$username.png' ?>" />

I didnt really think that would work but Im not exactly sure where to go on this one I have a java application that saves the images to a file on the users computer then uploads them to my server and saves them all as "img_someusername.png" but the username is going to be different for every person

how can I get it so that the user that is logged in will see his or her picture and not someone else's much like a profile picture on facebook?

4

5 回答 5

1

试试这个。

<img src="screenshots/img_<?php echo $username; ?>.png" />
于 2013-07-23T22:18:43.480 回答
1
<?php
    echo "<img src='screenshots/img_".$username."'>";
?>
于 2013-07-23T22:19:27.273 回答
0

当您的 java 程序正在使用时,它会自动保存带有扩展名的图像,因此您可能不需要添加扩展名。Perhpas shawn.jpg 或 sandra.jpg。

这就是我会做的。

<img src="<?php echo 'screenshots/img_'.$usernames; ?>" />

其他用户已经提到了也可以添加 .png 的方式。

如果不是,这也可以

<img src="<?php echo 'screenshots/img_'.$usernames.'.png'; ?>" />

谢谢。

于 2013-07-23T22:25:01.703 回答
0

问题是您试图在单引号 (') 字符串中评估 PHP 变量。您只能在双引号字符串 (") 内评估变量。假设图像的路径正确,将代码更改为此应该没问题。

<img src="<?php echo "screenshots/img_$username.png" ?>" />

或者,其他两个答案也可以。PHP中有很多选项。

于 2013-07-23T22:22:53.073 回答
0

如果您的字符串包含一个变量,最好使用双引号来回显它。在这里,我将 src 属性用单引号括起来,然后将 echo 语句本身用双引号括起来。

<img src='<?php echo "screenshots/img_{$username}.png" ?>' />

另一种解决方案是连接变量:

<img src="<?php echo 'screenshots/img_'. $username ; ?>" />

希望这可以帮助!

于 2013-07-23T22:24:17.697 回答