PDA

View Full Version : using php to check filenames


fogofogo
02-20-2006, 10:08 AM
Hello folks.

I have a question about using php to select a file according to date. I am working on a project that uses xml to display horoscopes on a website. The xml feeds are ftp'd onto our server and have a different file name depending on the date. Unfortunetly this is the only way the xml is available.

The filenaming is:

Feed_name_YYYYMMDDHHMMSS_$storyid.xml

For example:
TMG_Horoscopes_Daily_Virgo_media_20060216100017_985313.xml


I'll be using php to do this, and as I'm fairly new to it, I'm not sure where to go from here. Does anyone have any advice, scripts or tutorials that might be able to help me?

Thanks guys,

John

raf
02-20-2006, 10:20 AM
i don't realy understand the datetime part (--> do you have a different xml file for each second (in combination with each storyid and feedname?)

and what exactly do you wanna do with the xml? what do you mean by 'select a file'?

anyhow, building your flename will look like

$filename=$feedname .'_'. date(YmdGis) .'_'. $stotyid .'.xml';

fogofogo
02-20-2006, 12:44 PM
Thanks for the reply Raf - sorry my earlier post was a little vague.

The xml files are submitted to us by a third party company, and this is their naming convention (to included the date/time as well). Its annoying, but thats the way they do it:

Feed_name_YYYYMMDDHHMMSS_$storyid.xml

The feed name always stays the same, as does the $storyid.

Thanks for that code, I'll try it out.


I have another problem though.

Here is my XML:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE feed SYSTEM "http://dtd.rivalsdm.com/lifestyle/news/1.0">
<feed vendorName="TMG" id="TMG_Lifestyle_Horoscopes_Daily_media" status="NORMAL">
<!-- for internal rivals use -->
<feed-info comp-id="LF_HOR_RUS" event-type="MMSTAU" gen-date=""/>
<content.item id="TMG_Horoscopes_Daily_Taurus_media_985297">
<content.meta>
<author>TMG</author>
<category>Lifestyle</category>
<copyright>TMG</copyright>
<vendor>TMG</vendor>
</content.meta>
<content.head>
<title>Taurus</title>
<dateline>15/02/2006 10:00 +0000</dateline>
<properties>
<prop prop-id="cb_hierachy">/MOBILE/NON-SPORTS/HOROSCOPES/DAILY/TAURUS</prop>
</properties>
</content.head>
<content.body>

<media media-type="image" media-mimetype="image/jpeg" media-format="SMALL">
<media-ref src="http://synd1.teamtalk.com/Images/horoscope_images/taurus.gif" />
<media-cap>Taurus</media-cap>
</media>

<article>
<ShortVersion>
<ShortHeadline>Taurus Normal</ShortHeadline>
<ShortBodytext>
<Para>You could be persuaded into helping a charity or community venture. Fighting for a good cause is just the challenge you need now to make you feel you are making a difference in other people&apos;s lives. That said: if you don&apos;t make a move on a family related matter, you may have to argue your case all over again with those who are now on your wavelength.</Para>
</ShortBodytext>
</ShortVersion>
</article>
</content.body>
</content.item>
</feed>



I am trying to select <Para> and the <title> tags and I'm having a problem. I cant seem to set the tag keys properly to get the information;

for example: "*FEED*FEED-INFO*CONTENT.ITEM*CONTENT.HEAD*TITLE

Heres my php code:

<?php

$xml_file = "TMG_Horoscopes_Daily_Taurus_media_20060215100011_985297.xml";

$xml_headline_key = "*FEED*FEED-INFO*CONTENT.ITEM*CONTENT.HEAD*TITLE";
$xml_description_key = "*FEED*FEED-INFO*CONTENT.ITEM*CONTENT.BODY*ARTICLE*SHORTVERSION*SHORTBODYTEXT*PARA";

$story_array = array();

$counter = 0;
class xml_story{
var $headline, $description;
}

function startTag($parser, $data){
global $current_tag;
$current_tag .= "*$data";
}

function endTag($parser, $data){
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}

function contents($parser, $data){
global $current_tag, $xml_headline_key, $xml_description_key, $counter, $story_array;
switch($current_tag){
case $xml_headline_key:
$story_array[$counter] = new xml_story();
$story_array[$counter]->headline = $data;
break;
case $xml_description_key:
$story_array[$counter]->description = $data;
$counter++;
break;
}
}

$xml_parser = xml_parser_create();

xml_set_element_handler($xml_parser, "startTag", "endTag");

xml_set_character_data_handler($xml_parser, "contents");

$fp = fopen($xml_file, "r") or die("Could not open file");

$data = fread($fp, filesize($xml_file)) or die("Could not read file");

if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}

xml_parser_free($xml_parser);

fclose($fp);

?>

<html>
<head>
<title>Horoscopes</title>
</head>
<body bgcolor="#FFFFFF">
<?php
for($x=0;$x<count($story_array);$x++){
echo "\t<h2>" . $story_array[$x]->headline . "</h2>\n";
echo "\t\t\n";
echo "\t<i>" . $story_array[$x]->description . "</i>\n";
}
?>

</body>
</html>

When the page is loaded, just a blank page is returned. I've probably done something incredibly stupid, sorry if it obvious, but I cant figure it out.

Thanks

John

chump2877
02-20-2006, 01:54 PM
If you're looking for a nice PHP class to parse your XML/RSS, try this out: http://www.hotscripts.com/Detailed/48456.html

I've used an earlier version of this class, and it works well....It's actually designed for RSS, but, if you know what you;re doing, the class can be extended to include additional tags (to read other XML docs)....

Anyway, seems like a good PHP framework for parsing XML...even tho I've only used it for RSS feeds...

fogofogo
02-21-2006, 11:52 AM
i don't realy understand the datetime part (--> do you have a different xml file for each second (in combination with each storyid and feedname?)

and what exactly do you wanna do with the xml? what do you mean by 'select a file'?

anyhow, building your flename will look like

$filename=$feedname .'_'. date(YmdGis) .'_'. $stotyid .'.xml';



Hello again raf,

Basically the time part of the file name will be different each time. ( a full timestamp is used to prevent overwriting the file if there is a correction)

So the date part of the file name will depend on the days date, and then a check will need to be made to select the most recent file according to the time part of the file name. Does this make sense?

its based on Feed_name_YYYYMMDDHHMMSS_$storyid.xml

(TMG_Horoscopes_Daily_Taurus_media_20060215100011_985297.xml)

Anyone have any ideas how this could be done? Or know of any handy scripts I could use?

Heres my code:

<?php
// read xml file into string
$xml_file = "TMG_Horoscopes_Daily_Taurus_media_20060215100011_985297.xml";
$text = file_get_contents($xml_file) or die("read file failed");

// parse the xml into structure
$p = xml_parser_create();
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($p, $text, $vals, $index);
xml_parser_free($p);

// read data of interest from structure into array:
for($i=0; $i<$index['content.item'][1]; $i++)
{
foreach(array('title', 'Para') as $key)
{
$data[$i][$key] = $vals[$index[$key][$i]]['value'];
}
}



foreach($data as $val)
{
echo "\t<h2>" . $val['title'] . "</h2>\n";
echo "\t\t\n";
echo "\t<i>" . $val['Para'] . "</i>\n";
}

?>

degsy
02-21-2006, 01:57 PM
So are you wanting a function to sort the files for you?

You could loop through the files in the folder and put them into an array.
You can also extract the date part and put that in an array.

You can then sort by the date.

http://uk2.php.net/readdir
http://uk.php.net/manual/en/function.asort.php