Jump to content

using glob() to look for all txt in directory


Imaulle

Recommended Posts

Hello,

 

I'm trying to make a little piece of code that will search for all files (including subdirectories) ending with .txt and set them to 777

 

 

foreach (glob("*.txt") as $file) {
system("chmod 777 '$file'");
}

 

 

Am I on the right path?

 

 

 

thanks

Link to comment
Share on other sites

Close, however that will only get you the files in the current directory (not in any sub-directories). Try this:

 

<?php

$directory = 'sample';  // Replace with your starting directory
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));

while($it->valid())
{
    if(strtolower(substr($it->getFilename(), -4)) == '.txt')
    {
        var_dump($it->key());  // Not really needed, just used for debugging
        chmod($it->key(), 0777);
    }
    $it->next();
}

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.