Jump to content

Asking PHP to look for any NEW file?


scott.stephan

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/167981-asking-php-to-look-for-any-new-file/
Share on other sites

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()

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;

?>

Archived

This topic is now archived and is closed to further replies.

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