Jump to content


Photo

finding file in directory


  • Please log in to reply
1 reply to this topic

#1 croakingtoad

croakingtoad
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts
  • LocationUSA

Posted 14 March 2006 - 04:41 PM

I have a service that is ftp-pushing one file per day into my site and I need to write some PHP to set the filename as a variable.

Here's the structure of a file's name--

rva-1.20060313.1107.zip

The first part 'rva-1' will always be that. The second part '20060313' is obviously the current date and I can easily calculate that. The last part 'zip' will also always be that.

The problem is the '1107'. That is a time stamp and will likely always be different.

I suppose I need to create an array by searching the directory with something like
$array = scandir(/tmp);

That could be completely the wrong way to start, can someone give me a hand on how to find out first if that file is in there yet, and if so, to set a var of $fileName with the file's name...

Thanks in advance!

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 14 March 2006 - 06:06 PM

To check if the file is actually in the directory I would try something like this, which is the best I can think of right now - anyone else have any better ideas?
<?php
// Set some vars
$dir = "/public/uploads/";
$found = 0;
$date = date("Ymd");

// Open a known directory, and proceed to read its contents and check for the presence of a partial filename
if(is_dir($dir)) {
   if($dh = opendir($dir)) {
       while((($file = readdir($dh)) !== false) && $found == 0) {
           if(strpos($file,"rva-1.$date.")) {
              $found = 1;
              $filename = $file;
           }
       }
       closedir($dh);
   }
}

if(isset($filename)) echo "Todays file was found, it is called <b>$filename</b>";
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users