davefootball123 Posted January 18, 2014 Share Posted January 18, 2014 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; } Quote Link to comment https://forums.phpfreaks.com/topic/285477-get-latest-file-in-directory/ Share on other sites More sharing options...
ginerjm Posted January 18, 2014 Share Posted January 18, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285477-get-latest-file-in-directory/#findComment-1465688 Share on other sites More sharing options...
doddsey_65 Posted January 18, 2014 Share Posted January 18, 2014 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; } Quote Link to comment https://forums.phpfreaks.com/topic/285477-get-latest-file-in-directory/#findComment-1465690 Share on other sites More sharing options...
davefootball123 Posted January 18, 2014 Author Share Posted January 18, 2014 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, Quote Link to comment https://forums.phpfreaks.com/topic/285477-get-latest-file-in-directory/#findComment-1465691 Share on other sites More sharing options...
jazzman1 Posted January 18, 2014 Share Posted January 18, 2014 (edited) 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 January 18, 2014 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/285477-get-latest-file-in-directory/#findComment-1465692 Share on other sites More sharing options...
davefootball123 Posted January 18, 2014 Author Share Posted January 18, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285477-get-latest-file-in-directory/#findComment-1465693 Share on other sites More sharing options...
doddsey_65 Posted January 19, 2014 Share Posted January 19, 2014 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.*') Quote Link to comment https://forums.phpfreaks.com/topic/285477-get-latest-file-in-directory/#findComment-1465699 Share on other sites More sharing options...
davefootball123 Posted January 19, 2014 Author Share Posted January 19, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285477-get-latest-file-in-directory/#findComment-1465719 Share on other sites More sharing options...
jazzman1 Posted January 19, 2014 Share Posted January 19, 2014 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/285477-get-latest-file-in-directory/#findComment-1465746 Share on other sites More sharing options...
davefootball123 Posted January 19, 2014 Author Share Posted January 19, 2014 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); } Quote Link to comment https://forums.phpfreaks.com/topic/285477-get-latest-file-in-directory/#findComment-1465785 Share on other sites More sharing options...
jazzman1 Posted January 19, 2014 Share Posted January 19, 2014 I was thinking that you're the owner or at least having an access to "dd.weatheroffice.gc.ca". Glob and directoryOperator works locally. Quote Link to comment https://forums.phpfreaks.com/topic/285477-get-latest-file-in-directory/#findComment-1465798 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.