Jump to content

Get Latest File In Directory


davefootball123

Recommended Posts

Hello, I am having a bit of trouble with a preg_match statement. I have a directory of files at http://dd.weatheroffice.ec.gc.ca/meteocode/ont/cmml/ and I am trying to get the latest FPT011 file in the directory.  I have some code below showing what I am trying to accomplish however I'm having trouble with the last part of the preg_match statement. Any help would be greatly appreciated.

 

Thanks,

$url = 'http://dd.weatheroffice.ec.gc.ca/meteocode/ont/cmml/';
$contents = file_get_contents($url);

preg_match_all('/(?<=")TRANSMIT.FPT011............(?=")/', $contents, $result);


foreach ($result as $latestbulletin) {
$latest = end($latestbulletin);
echo $latest;

}
Link to comment
Share on other sites

Isn't your url a path to a folder?  Can you then read the contents of a whole folder with file_get_contents?  From my reading,  f_g_c reads a 'file'.

 

I think you need to use glob on that $url and then go thru each one looking at the last modified date or whatever criteria you want to use.

Link to comment
Share on other sites

As said just use glob to iterate over all the folders files and get the latest using filemtime (file modified time).

$latest_file = null;
foreach (glob('meteocode/ont/cmml/*') as $file)
{
    if ($latest_file === null || filemtime($file) > $latest_file)
        $latest_file = $file;
}
Link to comment
Share on other sites

 

As said just use glob to iterate over all the folders files and get the latest using filemtime (file modified time).

$latest_file = null;
foreach (glob('meteocode/ont/cmml/*') as $file)
{
    if ($latest_file === null || filemtime($file) > $latest_file)
        $latest_file = $file;
}

Really appreciate all of the help! Will this work to just grab the FTP011 files though?

 

 

Thanks,

Link to comment
Share on other sites

Why don't you test the script provided by doddsey_65 rather than repeat the same question twice ???

As ginerjm stated you would use a glob() function. I think that the easiest way. Go to the source of php.net and take a look at examples. Make some effort trying to write the script yourself and if you got obstacles come back again. 

Edited by jazzman1
Link to comment
Share on other sites

Why don't you test the script provided by doddsey_65 rather than repeat the same question twice ???

As ginerjm stated you would use a glob() function. I think that the easiest way. Go to the source of php.net and take a look at examples. Make some effort trying to write the script yourself and if you got obstacles come back again. 

Thanks for your help as well, have been looking at examples for quite some time. 

Link to comment
Share on other sites

 

glob() will get any files you want. As you can see I specified a wildcard (*) meaning pull all files in the directory. In order to get only the ones you want you could use the wildcard with a prefix:

glob('meteocode/ont/cmml/TRANSMIT.FTP011.*')

Really appreciate all the help. I have been playing around with some code for the past couple of hours and am wondering why glob() returns empty when sorting by file modification time. It seems like the time stamped data isn't being read. The way I had previously done this for a directory was get the content of the folder and sort the filenames, however the preg_match statement is a bit more complicated for this one. I will try a few more things then report back..

Once agian, thanks for the help.

Link to comment
Share on other sites

The doddsey_65's script should work for you b/s it works for me. Double-check the path to the target directory (use an absolute path for every case).

 

It's my attempt using DirectoryIterator.

<?php

$dirTarget = 'directory/target/';

$searchTime = array();

$date = new DateTime();

$iterator = new DirectoryIterator($dirTarget);

foreach ($iterator as $fileinfo) {
    if ($fileinfo->isFile()) {
     $searchTime[]=$fileinfo->getMTime().' '.$fileinfo->getFilename();
    }   
}

sort($searchTime,SORT_NUMERIC);

$result = explode(" ",end($searchTime));

$date->setTimestamp($result[0]);

echo "<pre>"
. "Last modifided file is: ".$result[1]."\n".
'Date modified is:'.$date->format('Y-m-d H:i:s') . "\n".
 "</pre>";
Link to comment
Share on other sites

Ended up getting the preg_match statement correct to find the name of the latest bulletin. 

 

Thanks for the help, everyone. 

$url = 'http://dd.weatheroffice.gc.ca/meteocode/ont/cmml/?C=M;O=A';
$contents = file_get_contents($url);

preg_match_all('/(?<=")TRANSMIT.FPTO11.*(?=")/', $contents, $result);


foreach ($result as $latestbulletin) {
$latest = end($latestbulletin);

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.