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.

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

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面