scott.stephan Posted July 29, 2009 Share Posted July 29, 2009 I have a small issue- Everyday a .csv file is published to a server. This .csv file has an unpredictable name, but it does show up everyday at the same time and after it's processed it gets moved to an "Archive" directory. Is it possible to tell PHP "Look for any new *.csv" file. As far as I can tell, the fgetcsv requires a $handle. Is there a way to read the directory and get the name of the CSV sitting in it? Quote Link to comment https://forums.phpfreaks.com/topic/167981-asking-php-to-look-for-any-new-file/ Share on other sites More sharing options...
FD_F Posted July 29, 2009 Share Posted July 29, 2009 try this to get file name for handle: $Files = array(); if ($handle = opendir('/home/mydir')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $Files[] = $file; } } closedir($handle); } $f = array(); foreach($Files as $file){ $f[] = explode(".",basename($file)); } foreach($f as $csv) { if($csv[1] == 'csv') { $handle = "{$csv[0]}.{$csv[1]}"; } } echo $handle ; // this is the file name for fopen() Quote Link to comment https://forums.phpfreaks.com/topic/167981-asking-php-to-look-for-any-new-file/#findComment-886036 Share on other sites More sharing options...
Maq Posted July 29, 2009 Share Posted July 29, 2009 Try something like: (Not tested) $path = "./"; foreach (glob($path . "/*.csv") as $filename) { if (filemtime($filename) >= $recentDate) { recentDate = filemtime($filename); $recentFile = $filename; } } echo "Most recent file: " . $filename; ?> Quote Link to comment https://forums.phpfreaks.com/topic/167981-asking-php-to-look-for-any-new-file/#findComment-886115 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.