2016-12-05 100 views
1

通過XML加載它時,不會顯示在瀏覽器中的圖像我有這樣的data.xml文件嘗試使用PHP

<?xml version="1.0" encoding="UTF-8"?> 
<TVchannel> //This is the root element that consists of IT, PTG and HR tv 

    <monthname month="September"> 

     <channelname name="IT"> 
      <title>Welcome to IT TV</title> 
     <images> 
     <image path="image1.png"/> 
     <image path="image2.png"/> 
     </images> 
     </channelname> 

     <channelname name="PTG"> 
      <title>Welcome to PTG TV</title> 
     <images> 
     <image path="image3.jpeg"/> 
     <image path="image4.jpeg"/> 
     </images> 
       </channelname> 

     <channelname name="HR"> 
      <title>Welcome to HR TV</title> 
     <images> 
     <image path="image5.jpeg"/> 
     <image path="image6.jpeg"/> 
     </images> 
     </channelname> 

    </monthname> 

    <monthname month="October"> //the view for a different month 

     <channelname name="IT"> 
      <title>Welcome to IT TV</title> 
     </channelname> 

     <channelname name="PTG"> 
      <title>Welcome to PTG TV</title>  
     </channelname> 

     <channelname name="HR"> 
      <title>Welcome to HR TV</title>   
     </channelname> 

    </monthname> 

</TVchannel> 

我有一個使用SimpleXML來加載該data.xml中這個PHP文件和迭代獲取每個圖像。

<?php 
    $picture_container = simplexml_load_file('data.xml'); 
    $title_bar = $picture_container->{'monthname'}[0]->{'channelname'}[0]->title; 
    echo $title_bar; 

header("content-type: image/png"); 
    foreach($picture_container->{'monthname'}[0]->{'channelname'}[0]->images->image as $iterator) 
    { 
     echo '<img src="'.$iterator['path'].'"/>';  

    } 

?> 

瀏覽器不顯示任何錯誤,但顯示一個空的圖標。 (參見圖) The browser does not show me any error but show an empty icon(see pic).The weird concentric rectangles!!!

請幫me..Thanks

+0

對我來說這是在瀏覽器打印'歡迎來到IT電視' – Exception

+0

你正在用'img'標籤生成一個HTML。內容類型錯誤,並且您之前已輸出內容。在瀏覽器的源視圖/開發人員工具中驗證生成的HTML輸出。確保所提供的URL上的瀏覽器可以找到該圖片。 – ThW

回答

1

由於您所指定的屬性路徑,在foreach循環使用->attributes() :)

echo $iterator->attributes()->path; 
+0

我替換了echo'';回聲'

+0

@AakashTiwari只是確保路徑是正確的,當然你需要指出正確的目錄,如果它在相同的目錄,那麼'image5.jpeg'應該足夠了,只要確保該文件是可讀的 – Ghost

-2

解決 寫這

echo '<img src="'.$iterator['path'].'" height="100"; "width="100" ;>'; 
+0

如何;在img標籤的末尾工作?這是錯誤的語法 – Exception