Skip to content

Instantly share code, notes, and snippets.

@yuseferi
Last active September 4, 2018 06:49
Show Gist options
  • Save yuseferi/ecd34812abec0cd2e5478c6e2d124ddc to your computer and use it in GitHub Desktop.
Save yuseferi/ecd34812abec0cd2e5478c6e2d124ddc 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