duanci1858 2016-03-22 10:34
浏览 154
已采纳

通过php在LocalHost上img的绝对路径

Working on a personal project, having some confusion with img src attribs being supplied by in-line php. I also meant to ask, is in-line php generally look upon as poor practice?

Here's the code snip in any case

<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>

The issue I'm having is that the string it pumps out is

 <li>
 <img src="http://localhost/xampp/htdocs/hobnobv2/data/posts/liar/1.jpg"    >
 </li>

but is unable to load the image. The image exists in the directory.

C:\xampp\htdocs\hobnobv2\data\posts\liar

Any guidance you can give to me? I understand that the str_replace's are inefficient and I should probably be saving the "http://localhost/path/to/image.xml" string in my xml file, but is there a reason the image won't load?

Thanks everybody. My heart goes out to you all.

  • 写回答

1条回答 默认 最新

  • dongzhiji0814 2016-03-23 08:44
    关注

    There was an answer here but it's gone now.

    The issue was I had the straight up wrong URL.

    http://localhost/hobnobv2/data/posts/liar/1.jpg
    

    rather than

    http://localhost/xampp/htdocs/data/posts/liar/1.jpg
    

    Thanks to the guy who posted this originally, lost to the sands of time. Hope this helps someone.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程