Jump to content

Dynamic Array using glob?


headmine

Recommended Posts

Is there an easier way to do this?

 

I am trying to get create a dyamic array based on files within the folder.

 

foreach (glob("*.jpg") as $filename) {
   $items = array("title" => $filename)
}

    $output = '<?xml version="1.0" encoding="ISO-8859-1"?>';
    $output .= '<rss version="2.0">';
    $output .= "<channel>";
    $output .= "<title>" . $channel["title"] . "</title>";
    $output .= "<description>" . $channel["description"] . "</description>";
    $output .= "<link>" . $channel["link"] . "</link>";
    $output .= "<copyright>" . $channel["copyright"] . "</copyright>";

    foreach ($items as $item) {
        $output .= "<item>";
        $output .= "<title>" . $item["title"] . "</title>";
        $output .= "</item>";
    }
    $output .= "</channel>";
    $output .= "</rss>";

    header("Content-Type: application/rss+xml; charset=ISO-8859-1");
    echo $output;
?>

 

This doesn't even work correctly.

Link to comment
https://forums.phpfreaks.com/topic/184578-dynamic-array-using-glob/
Share on other sites

What im trying to do is create a dynamic rss image feed based on images within a folder.

 

Your array fix helped me but its . Still not working

 

foreach (glob("*.jpg") as $filename) {
   $items[] = array("img_local" => $filename);
}

    $output = '<?xml version="1.0" encoding="ISO-8859-1"?>';
    $output .= '<rss version="2.0">';
    $output .= "<channel>";
    $output .= "<title>" . $channel["title"] . "</title>";
    $output .= "<description>" . $channel["description"] . "</description>";
    $output .= "<link>" . $channel["link"] . "</link>";
    $output .= "<copyright>" . $channel["copyright"] . "</copyright>";

    foreach ($items as $item) {
        $output .= "<item>";
        $output .= "<content><img src=\"http://LOCATION/" . $item["img_local"] . "\" /></content>";
        $output .= "</item>";
    }
    $output .= "</channel>";
    $output .= "</rss>";

    header("Content-Type: application/rss+xml; charset=ISO-8859-1");
    echo $output;

 

thats because your <item> should be embedded in another tag say <items> <item>blah blah</item> </items

 

I don't understand?

 

Like this

 

 foreach ($items as $item) {
	$output .= "<items>";
        $output .= "<item>";
        $output .= "<content><img src=\"http://LOCATION/" . $item["img_local"] . "\" /></content>";
        $output .= "</item>";
	$output .= "</items>";
    }

I've made a few edits....

 

Here is the link to the page: http://headmine.org/the-test/index.php

 

I've only been able to get the icons to show with links. I'd like the images to display.

 

here is the updated code

 

<?php
    $channel = array("title"        => "Headmine",
                     "description"  => "Example of a RSS feed.",
                     "link"         => "http://headmine.org",
                     "copyright"    => "Copyright (C) 2009 headmine.org");

foreach (glob("*.jpg") as $filename) {
   $items[] = array("img" => $filename);
}

    $output = '<?xml version="1.0" encoding="ISO-8859-1"?>';
    $output .= '<rss version="2.0">';
    $output .= "<channel>";
    $output .= "<title>" . $channel["title"] . "</title>";
    $output .= "<description>" . $channel["description"] . "</description>";
    $output .= "<link>" . $channel["link"] . "</link>";
    $output .= "<copyright>" . $channel["copyright"] . "</copyright>";

    foreach ($items as $item) {
	$output .= "<item>";
        $output .= "<enclosure length=\"1234567\" type=\"image/jpeg\" url=\"http://johnregalado.com/the-test/". $item["img"] ."\" />";
	$output .= "<url>http://headmine.org/the-test/". $item["img"] ."</url>";
    $output .= "</item>";
}

    $output .= "</channel>";
    $output .= "</rss>";

    header("Content-Type: application/rss+xml; charset=utf-8");
    echo $output;

?>

Sorry output is:

 

<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Headmine</title><description>Example of a RSS feed.</description><link>http://headmine.org</link><copyright>Copyright (C) 2009 headmine.org</copyright><item><enclosure length="1234567" type="image/jpeg" url="http://johnregalado.com/the-test/a-river-runs-though-it.jpg" /><url>http://headmine.org/the-test/a-river-runs-though-it.jpg</url></item><item><enclosure length="1234567" type="image/jpeg" url="http://johnregalado.com/the-test/mark-ruhi.jpg" /><url>http://headmine.org/the-test/mark-ruhi.jpg</url></item><item><enclosure length="1234567" type="image/jpeg" url="http://johnregalado.com/the-test/mark.jpg" /><url>http://headmine.org/the-test/mark.jpg</url></item><item><enclosure length="1234567" type="image/jpeg" url="http://johnregalado.com/the-test/namrok.jpg" /><url>http://headmine.org/the-test/namrok.jpg</url></item></channel></rss>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.