Last active
September 12, 2023 23:22
-
-
Save endurtech/88c5e000898dcd3d55be62be3c4668eb to your computer and use it in GitHub Desktop.
Simple php script sets correct directory and file permissions on WordPress and others. Directories are 755, files are 644, .htaccess and wp-config.php is 444.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
** Plugin Name: WordPress Permissions Fixer | |
** Plugin URI: https://endurtech.com/how-to-fix-wordpress-file-and-folder-permissions/ | |
** Description: Simple php script sets correct directory and file permissions on WordPress and others. Directories are 755, files are 644, wp-config.php is 444. | |
** Author: Manuel Rodrigues | |
** Author URI: https://endurtech.com | |
** Version: 1.1 | |
** Tags: wordpress, php, script, directory, directories, file, files, wp-config.php, chmod, fix | |
** License: GPL-2.0+ | |
*/ | |
file_fix_directory( dirname(__FILE__) ); | |
function file_fix_directory( $dir, $nomask = array( '.', '..' ) ) | |
{ | |
if ( is_dir( $dir ) ) | |
{ | |
// Reset directories | |
if ( @chmod( $dir, 0755 ) ) | |
{ | |
echo "<p>Fixed: " . $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 | |
file_fix_directory( "$dir/$file", $nomask ); | |
} | |
else | |
{ | |
$filename = "$dir/$file"; | |
// Reset files | |
if ( @chmod( $filename, 0644 ) ) | |
{ | |
echo "<p>Fixed: " . $filename . "</p>"; | |
} | |
} | |
} | |
} | |
closedir( $handle ); | |
} | |
} | |
unlink(__FILE__); | |
echo "<p> </p> | |
<p>Your Directory and File Permissions have been Reset.<br />Please close this window and remove the <span style=\"background-color:#cccccc;\">permissions-fix.php</span> file from your webserver.</p>"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice script. Thanks for sharing. :-)
I would like to share a library I have coded with the same goal.
Example of usage:
Full documentation: https://github.com/MathiasReker/php-chmod