Jump to content

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;

?>

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.