Jump to content

finding file in directory


croakingtoad

Recommended Posts

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
[code]
$array = scandir(/tmp);
[/code]

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!
Link to comment
https://forums.phpfreaks.com/topic/4947-finding-file-in-directory/
Share on other sites

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?
[code]<?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>";
?>[/code]

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.