Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save teknikqa/52826ab0dfc105a840549fdbb6d06e8c to your computer and use it in GitHub Desktop.
Save teknikqa/52826ab0dfc105a840549fdbb6d06e8c to your computer and use it in GitHub Desktop.
Fix Files Directories Permissions by PHP code
<?php
fix_file_directory_permission(dirname(__FILE__));
function fix_file_directory_permission($dir, $nomask = array('.', '..')) {
if (is_dir($dir)) {
// Try to fix directories permission
if (@chmod($dir, 0755)) {
echo "<p>Permission Fixed for : " . $dir . "</p>";
}
}
if (is_dir($dir) && $handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (!in_array($file, $nomask) && $file[0] != '.') {
if (is_dir("$dir/$file")) {
// Recurse into subdirectories
fix_file_directory_permission("$dir/$file", $nomask);
}
else {
$filename = "$dir/$file";
// Try to fix files permission
if (@chmod($filename, 0644)) {
echo "<p>Permission Fixed for : " . $filename . "</p>";
}
}
}
}
closedir($handle);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment