Don S
Don S

Reputation: 134

How to display php output as xml

I have this simple php that outputs a list of all the png files in a directory. Is there a way to take this list and make it xml?

<?php
foreach (glob("*.png") as $filename) {
    echo "$filename " ;
}
?>

Upvotes: 0

Views: 194

Answers (2)

Andreas Wong
Andreas Wong

Reputation: 60516

Render it manually?

<?php
$xmlStr = '<?xml version="1.0" ?><directory>';

foreach (glob("*.png") as $filename) {
    $xmlStr .= '<file>' . $filename . '</file>';
}
$xmlStr .= '</directory>';

header('Content-type: text/xml');
echo $xmlStr;
?>

Upvotes: 1

robbrit
robbrit

Reputation: 17960

Depends on the format of XML that you want. This would work:

<?xml version="1.0" encoding="UTF-8"?>
<?php
foreach (glob("*.png") as $filename) {
    echo "<png>$filename</png>";
}
?>

Upvotes: 2

Related Questions