在php中的浏览器上打印xml文本


print xml text on browser in php

有了一个装满图像的目录,现在我想在php浏览器中打印类似于以下xml行的内容

<image>
    <thumb_path>amor/thumbs/1_cuadro_de_amor.jpg</thumb_path>
    <image_path>amor/1_cuadro_de_amor.jpg</image_path>
    <transition_type>swipe_from_left_to_right</transition_type>
    <transition_duration>1</transition_duration>
    <transition_delay>3</transition_delay>
    <description_window from="left" to_x="55" to_y="0" duration="1" delay="0"><![CDATA[<p class="descRed1_1">1.- Cuadro de amor</p>]]></description_window>
  </image>

所以我创建了一个php文件并上传到

$dir = 'amor';
$count=1;
foreach (glob($dir.'/*.*') as $filename)
{
    if($filename=="." || $filename==".." || $filename=="download.php" || $filename=="index.php")
    {
        //this will not display specified files
    }
    else
    {
        echo 'header ("Content-Type:text/xml");';
        echo '<image>';
        echo '  <thumb_path>'.$dir.'/thumbs/'.$filename.'</thumb_path>';
        echo '  <image_path>'.$dir.'/"'.$filename.'</image_path>';
        echo '  <transition_type>swipe_from_left_to_right</transition_type>';
        echo '  <transition_duration>1</transition_duration>';
        echo '  <transition_delay>3</transition_delay>';
        echo '  <description_window from="left" to_x="55" to_y="0" duration="1" delay="0"><![CDATA[<p class="descRed1_1">'.$count.'.- "'.ucfirst(str_replace("_"," ",$filename)).'</p>]]></description_window>';
        echo "</image>";
        echo "<br>";
        $count++;
    }
}

然而xml标记没有正确显示,我缺少什么我试着添加

echo 'header ("Content-Type:text/xml");';

但不起作用

我得到了amor/thumbs/amor/1_cuadro_amor.jpg amor/"amor/1_cuadro_amor.jpg从左到右滑动1 3 1.-"amor/10 cuadro de alegra.jpg

]]>

此外,我如何摆脱文件名上的dir字符串,在这种情况下是"amor"?

删除该语句的"echo"部分。你只想要

header("内容类型:text/xml");

你可能也想把这个放在最上面:

请确保第一个xml标记之前没有空格,否则会出现错误。