Solved

Read folder content and search for specific filenames

Posted on 2011-02-17
7
327 Views
Last Modified: 2012-06-27
Hi,


I have a php script to read a folder content:
<?php
if ($handle = opendir('attachments')) {
    while (($file = readdir($handle))!==false) {
		echo $file."<br/>";
    }
    closedir($handle);
}
?>

Open in new window

and txt files inside:
OUT.101217.143529.Maersk-Test.txt
OUT.110107.151627.Maersk-Test.txt
OUT.110217.220100.Maersk-Test.txt
IN.101217.143529.Maersk-Test.txt
IN.110107.151627.Maersk-Test.txt

What I need is:
- Recognize the latest 'OUT' file (sort by filename, then pickup latest one would be the easiest way I suppose), from files above that would be "OUT.110217.220100.Maersk-Test.txt";
- check the first number in filename, "110217" in this case (number between 'OUT.' and '.') and check if there exist 'IN' file with the same number, if so, echo 'OK', if not, echo 'MISSING DATA';
 
Example step by step:
1. recognize latest file with name beginning with 'OUT': OUT.110217.220100.Maersk-Test.txt
2. take first number from filename: 110217
3. check if there exist file with filename beginning with 'IN.110217'
4. if exists, print 'OK', if not, print 'MISSING DATA';


Thanks for any help
0
Comment
Question by:Zado
  • 4
  • 3
7 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34916882
You might be better using scandir() which does the same job as opendir/readdir but returns the files as an array of filenames. That way you can use the standard sort() routine as well

$fileArray = scandir('attachments');
arsort( $fileArray );

arsort will sort in REVERSE order so the OUT files are done first and the latest number (the biggest?) will be the first one.  Once you have the filename it is a trivial task to swap its OUT for IN and access the array

$filename = $fileArray [0];
$inFile = str_replace("OUT.", "IN.", $filename );

if ( isset( $fileArray [ $inFile ] ) )
    echo "OK";
else
    echo "MISSING DATA";

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34916895
0
 
LVL 8

Author Comment

by:Zado
ID: 34917115
Great idea, thanks, but there's something wrong in your script, no matter what happen, the script always prints 'MISSING DATA'
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34917814
Silly me, the data is in the array as VALUES not as KEYS. I should have used in_array. Replace the IF in the code fragment with

if ( in_array( $inFile, $fileArray ) )

like so


$fileArray = scandir('attachments');
arsort( $fileArray );
$filename = $fileArray[0];

$inFile = str_replace("OUT.", "IN.", $filename );

if ( in_array( $inFile, $fileArray ) )
    echo "OK";
else
    echo "MISSING DATA";

Open in new window

0
 
LVL 8

Author Comment

by:Zado
ID: 34918128
Thanks, almost done.
If filename is: OUT.110217.140029.Maersk-Test.ini
I'm interested if there exists IN.110217... in folder, I need to compare first number in filename only, not full filename, I've done some changes to your script, but don't know what to do next:

<?php
$fileArray = scandir('attachments');
arsort($fileArray);

$filename = $fileArray [0];
$subfilename = substr($filename, 4,6);

$inFile = str_replace("OUT.", "IN.", $filename );
$subinFile = substr($inFile, 3,6);

//I'm stuck here
if (in_array($subinFile,$fileArray)) { echo "OK"; }
else echo "MISSING DATA";
?>

Open in new window

0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 34918478
We need a custom comparison system so replace the in_array in the IF with this function and also please replace arsort with rsort (see code below)

function maerskCompare( $filename, & $fileArray ) {
     $found = false;
     $inFile = preg_replace('!^[\.A-Z]+([0-9]+).*$!', 'IN.$1', $filename );

     foreach( $fileArray as & $aFile )
          if ( strpos( $aFile, $inFile ) !== false ) {
               $found = true;
               break;
          }

     return $found;
}

// ...... other code .......

rsort( $fileArray );
$filename = $fileArray[0];

$inFile = str_replace("OUT.", "IN.", $filename );


if ( maerskCompare( $inFile, $fileArray ) )
    echo "OK";
else
    echo "MISSING DATA";

Open in new window



0
 
LVL 8

Author Comment

by:Zado
ID: 34918777
Nice, thanks a lot for your help.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now