Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Php file select question

Options
  • 21-02-2006 1:15pm
    #1
    Registered Users Posts: 597 ✭✭✭


    Hello,

    I am using php to select an xml document with the following file name structure:

    Feed_name_YYYYMMDDHHMMSS_$storyid.xml

    (example: daily_tv_20060215100011_985297.xml)

    Basically the date/time part of the file name will be different each time (sorry for stating the obvious! 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?

    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"; 
    } 
     
    ?>
    

    Thanks all.


Comments

  • Registered Users Posts: 2,157 ✭✭✭Serbian


    Are you trying to get the most recently modified file? If so, you could try something like this. This wasn't written precisely for what you are doing, so you could modify the function to find the most recently modified file and not have to do the second run which sorts the resulting array.

    [php]<?php
    /******************************
    * Initialise vars
    ******************************/

    $feed_directory = "feeds/";
    $error = array();

    /******************************
    * GetFiles Function
    ******************************/

    function GetFiles($directory) {

    // Declare the files array
    $files = array();

    // Check if the passed var is a directory
    if(is_dir($directory)) {
    // Open the directory
    $dirHandle = opendir($directory);

    // Get all the files in the directory
    while (($file = readdir($dirHandle)) !== false) {
    // Avoid . and ..
    if($file != "." && $file != "..") {
    // Get the next array key
    $next_key = sizeof($files);

    // Store file details
    $files[$next_key] = $file;
    $files[$next_key] = filetype($directory.$file);
    $files[$next_key] = date ("YmdHis", filemtime($directory.$file));

    // Clear the file status cache
    clearstatcache();
    }
    }
    }

    // Return the files array
    return $files;
    }

    /******************************
    * How to use this
    ******************************/

    $dateSort = array();
    $files = GetFiles($feed_directory);

    // Sort the files based on modified date
    if(is_array($files)&&sizeof($files)>0) {
    foreach($files as $key => $value) {
    $dateSort[$key] = $files[$key];
    }
    arsort($dateSort);
    }

    // Get the current key and print out the filename
    $key = each($dateSort);
    print "The most recently modified files is {$files[$key[0]]}";

    ?>[/php]


  • Registered Users Posts: 597 ✭✭✭yeraulone


    Serbian wrote:
    Are you trying to get the most recently modified file? If so, you could try something like this. This wasn't written precisely for what you are doing, so you could modify the function to find the most recently modified file and not have to do the second run which sorts the resulting array.

    [php]<?php
    /******************************
    * Initialise vars
    ******************************/

    $feed_directory = "feeds/";
    $error = array();

    /******************************
    * GetFiles Function
    ******************************/

    function GetFiles($directory) {

    // Declare the files array
    $files = array();

    // Check if the passed var is a directory
    if(is_dir($directory)) {
    // Open the directory
    $dirHandle = opendir($directory);

    // Get all the files in the directory
    while (($file = readdir($dirHandle)) !== false) {
    // Avoid . and ..
    if($file != "." && $file != "..") {
    // Get the next array key
    $next_key = sizeof($files);

    // Store file details
    $files[$next_key] = $file;
    $files[$next_key] = filetype($directory.$file);
    $files[$next_key] = date ("YmdHis", filemtime($directory.$file));

    // Clear the file status cache
    clearstatcache();
    }
    }
    }

    // Return the files array
    return $files;
    }

    /******************************
    * How to use this
    ******************************/

    $dateSort = array();
    $files = GetFiles($feed_directory);

    // Sort the files based on modified date
    if(is_array($files)&&sizeof($files)>0) {
    foreach($files as $key => $value) {
    $dateSort[$key] = $files[$key];
    }
    arsort($dateSort);
    }

    // Get the current key and print out the filename
    $key = each($dateSort);
    print "The most recently modified files is {$files[$key[0]]}";

    ?>[/php]

    Thanks man - I'll try that.


Advertisement