Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CHMOD Batch
#1
Credits and Source: http://www.dreamincode.net/

Name: CHMOD Batch
Description: This function lets you batch chmod an array of files or folders.
Snippet:
PHP Code:
<?php
// Permissions
$file_perms 0644;
$dir_perms 0755;

//
// This object lets you search for one or more files
// in one or more directories using regular expressions
// with the option to search sub directories and ignore case.
//

class file_search
{
  var 
$found = array();
  var 
$dirs = array();

  function 
file_search($files$dirs '.'$sub 1$case 0)
  {
    
$dirs = (!is_array($dirs)) ? array($dirs) : $dirs;
    foreach (
$dirs as $dir)
    {
      
$dir .= (!ereg('/$'$dir)) ? '/' '';
      
$directory = @opendir($dir);

      while ((
$file = @readdir($directory)) !== FALSE)
      {
        if (
$file != '.' && $file != '..')
        {
          if (
$sub && is_dir($dir $file))
          {
            
$this->dirs[] = $dir $file;

            
$this->file_search($files$dir $file$sub$case);
          }
          else
          {
            
$files = (!is_array($files)) ? array($files) : $files;
            foreach (
$files as $target)
            {
              
$tar_ext substr(strrchr($target'.'), 1);
              
$tar_name substr($target0strrpos($target'.'));
              
$fil_ext substr(strrchr($file'.'), 1);
              
$fil_name substr($file0strrpos($file'.'));

              
$ereg = ($case) ? 'ereg' 'eregi';
              if (
$ereg($tar_name$fil_name) && eregi($tar_ext$fil_ext))
              {
                
$this->found[] = $dir $file;
              }
            }
          }
        }
      }
    }
  }
}

function 
chmod_batch($array$chmod$display 1)
{
  if (empty(
$array))
  {
    return 
false;
  }

  foreach (
$array as $file)
  {
    if (
basename($file) == basename(__FILE__))
    {
      continue;
    }

    
$perms substr(sprintf('%o'$chmod), -4);
    
$fileperms substr(sprintf('%o'fileperms($file)), -4);

    
clearstatcache();

    if (
$perms != $fileperms)
    {
      
chmod($file$chmod);

      
$newperms substr(sprintf('%o'fileperms($file)), -4);
      
$message = ($newperms == $perms) ? '<b>success</b>' '<i>fail</i>';
    }
    else
    {
      
$message 'no change';
    }

    if (
$display)
    {
      echo 
"$file$fileperms -> $perms $message<br />\n";
    }
  }
}

// Search
$search = new file_search('^.+$.php');

// chmod files found
echo '<h3>Files</h3>';
chmod_batch($search->found$file_perms);

// chmod directories found
echo '<h3>Directories</h3>';
chmod_batch($search->dirs$dir_perms);
?>

Instructions: This function is based on the file_search class:
http://www.dreamincode.net/code/snippet930.htm

Set file and dir permissions appropriately. This uses php's chmod function, which requires that the permission setting be prefixed with a leading 0 as in the example.

Read more at http://www.php.net/chmod

At the bottom is the $search object using the file_search class. Read specifications in the Advanced File Search snippet to learn how to use this.

Following that is two uses of the chmod_batch function, first for files, second for directories. You can do both or comment out one or the other.

Thankyou for your reading, be happy always Smile
Reply


Messages In This Thread
CHMOD Batch - by zone - 11-06-2009, 05:59 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)