skip to Main Content

Let’s say I have a starting point folder called scan_inside. In the folder there are lots of sub-folders and in those sub-folders there might be even more folders with some content.

I would like to scan through all the folders and insert a file uploadme.xml if there is an index.htm file found in the current destination. How can I achieve this?

Illustration:

Scanning…

  • scan_inside:

    • subfolder1

      • style.css
      • logo.png
      • folder
        • homepage.htm
        • index.htm
        • uploadme.xml (must be uploaded because an index.htm file was found)
    • subfolder2

      • about.htm
      • logo.png
    • subfolder3

      • index.html
      • uploadme.xml (must be uploaded because an index.htm file was found)

2

Answers


  1. Chosen as BEST ANSWER

    I have updated the code a bit. It seeks for .php files and uploads an uploadme.xml to the same path. Seems to work quite alright but there might be some mistakes, though.

        function scanThroughDir($dir) {
            $result = [];
            foreach(scandir($dir) as $filename) {
                if ($filename[0] === '.') continue;
                $filePath = $dir . '/' . $filename;
                if (is_dir($filePath)) {
                    foreach (scanThroughDir($filePath) as $childFilename) {
                        $fileNameParts = explode('.', $childFilename);
                        if(end($fileNameParts) == "php"){
                            copy('uploadme.xml', pathinfo($filePath, PATHINFO_DIRNAME).'/uploadme.xml');
                            $result[] = $childFilename;
                        }
                    }
                } else {
                    $fileNameParts = explode('.', $filename);
                    if(end($fileNameParts) == "php"){
                        copy('uploadme.xml', pathinfo($filePath, PATHINFO_DIRNAME).'/uploadme.xml');
                    }
                }
            }
            return $result;
        }
    
    scanThroughDir("mainfolder");
    

  2. First, you need recursion, then you can go through all of the files and try to capture the extension. After that, you can add a file to the array.

    function scanThroughDir($dir) {
        $result = [];
        foreach(scandir($dir) as $filename) {
            if ($filename[0] === '.') continue;
            $filePath = $dir . '/' . $filename;
            if (is_dir($filePath)) {
                foreach (scanThroughDir($filePath) as $childFilename) {
                    $fileNameParts = explode('.', $childFilename);
    
                    if(end($fileNameParts) == "xml"){
                        echo end($fileNameParts);
                        $result[] = $childFilename;
                    }
                }
            } else {
                $fileNameParts = explode('.', $filename);
                if(end($fileNameParts) == "xml"){
                    $result[] = $filename;
                }
            }
        }
        return $result;
    }
    

    Usage

    print_r(scanThroughDir("./"));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search