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
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]
Link to comment
Share on other sites

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.