Skip to content

Instantly share code, notes, and snippets.

@uzielweb
Forked from anonymous/chmoder.php
Last active October 6, 2016 02:59
Show Gist options
  • Save uzielweb/7b3e7b25f655c44cdbcfffad20927602 to your computer and use it in GitHub Desktop.
Save uzielweb/7b3e7b25f655c44cdbcfffad20927602 to your computer and use it in GitHub Desktop.
CHMODER is a little script to chmod in a batch mode all files and directories recursively. How to use: upload to a server and run it via url. http://yoursite.etc/chmoder.php
<?
header('Content-Type: text/plain');
/**
* Changes permissions on files and directories within $dir and dives recursively
* into found subdirectories.
*/
function chmod_r($dir)
{
$dp = opendir($dir);
while($file = readdir($dp))
{
if (($file == ".") || ($file == "..")) continue;
$path = $dir . "/" . $file;
$is_dir = is_dir($path);
set_perms($path, $is_dir);
if($is_dir) chmod_r($path);
}
closedir($dp);
}
function set_perms($file, $is_dir)
{
$perm = substr(sprintf("%o", fileperms($file)), -4);
$dirPermissions = "0755";
$filePermissions = "0644";
if($is_dir && $perm != $dirPermissions)
{
echo("Dir: " . $file . "\n");
chmod($file, octdec($dirPermissions));
}
else if(!$is_dir && $perm != $filePermissions)
{
echo("File: " . $file . "\n");
chmod($file, octdec($filePermissions));
}
flush();
}
chmod_r(dirname(__FILE__));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment