【发布时间】:2016-03-23 16:44:39
【问题描述】:
从事个人项目,与内联 php 提供的 img src 属性有些混淆。我还想问,内联 php 通常被视为不好的做法吗?
这里是代码片段
<li>
<?php
$username = $_SESSION["username"];
echo($username);
?>
</li>
<li><img
<?php
$xml = new DomDocument("1.0");
if(file_exists("data/posts/$username/$username.xml")){
$xml->load("data/posts/$username/$username.xml");
$postsArray = $xml->getElementsByTagName('post');
}else{
echo ("Cannot reach image store");
}
foreach($postsArray as $post){
$profPic = $post->getElementsByTagName("profpic")[0]->nodeValue;
if($profPic == 'true'){
$imgPath = $post->getElementsByTagName("path")[0]->nodeValue;
$imgPath = str_replace("C:","localhost",$imgPath);
$imgPath = str_replace("\\","/", $imgPath);
echo ("src=\"http://".$imgPath."\"");
}
}
?>
>
</li>
我遇到的问题是它抽出的字符串是
<li>
<img src="http://localhost/xampp/htdocs/hobnobv2/data/posts/liar/1.jpg" >
</li>
但无法加载图像。图像存在于目录中。
C:\xampp\htdocs\hobnobv2\data\posts\骗子
您可以给我任何指导吗?我知道 str_replace 效率低下,我可能应该将“http://localhost/path/to/image.xml”字符串保存在我的 xml 文件中,但是图像无法加载是否有原因?
谢谢大家。我的心向你们所有人倾诉。
【问题讨论】: