0

I have problem with files, for example .342342.jpg or .3423423.ico. The script below dosen't see this files. My script:

<?php
$filepath = recursiveScan('/public_html/');

function recursiveScan($dir) {
    $tree = glob(rtrim($dir, '/') . '/*');
    if (is_array($tree)) {
        foreach($tree as $file) {
            if (is_dir($file)) {
                //echo $file . '<br/>';
                //recursiveScan($file);
            } elseif (is_file($file)) {
 echo $file . '<br/>'; 
               if (preg_match("[.a-zA-Z0-9]", $file )) {
                   echo $file . '<br/>';
                   //unlink($file);
               }

            }
        }
    }
}
?>
Adam Kowalski
  • 103
  • 13
  • 1
    What is your pattern supposed to match, with your words? – Cid May 11 '18 at 08:00
  • Possible duplicate of [PHP RecursiveDirectoryIterator](https://stackoverflow.com/questions/20045622/php-recursivedirectoryiterator) – Robert May 11 '18 at 08:03

2 Answers2

0

Use this \.[[:alnum:]]*as your regular expression to match a single dot and then any number of letters and digits afterwards, because as you're using it now it only matches a single character of any kind. Use regex101.com for future regular expression testing. It shows a detailed breakdown of what you're filtering for and has a great cheatsheet for all tokens you can use

0

AFAIK, glob doesn't return filenames that begin with a dot, so, .342342.jpg is not returned.

Your regex if (preg_match("[.a-zA-Z0-9]", $file )) { matches filenamess that contain .a-zA-Z0-9 (ie. xxx.a-zA-Z0-9yyy) I guess you want filenames that contain dot or alphanum, so your regex becomes:

if (preg_match("/^[.a-zA-Z0-9]+$/", $file )) {
Toto
  • 89,455
  • 62
  • 89
  • 125
  • Something is wrong. My file has name: .05becc52.ico and the code if (preg_match("/^[.0-9a-z0-9]+$/", $file )) { of course I modify glob to display this file and it is working. – Adam Kowalski May 14 '18 at 07:18
  • Function glob return the file with dot for example: .05becc52.ico. But the preg_match dosent work. – Adam Kowalski May 14 '18 at 21:49
  • @AdamKowalski: How do you make `glob` matches files that begin with dot? For me it doesn't return these files. You'd better use [scandir](http://php.net/manual/en/function.scandir.php) – Toto May 15 '18 at 09:17