Skip to content

Instantly share code, notes, and snippets.

@endurtech
Last active July 11, 2025 14:07
Show Gist options
  • Save endurtech/88c5e000898dcd3d55be62be3c4668eb to your computer and use it in GitHub Desktop.
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.
<?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>&nbsp;</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>";
?>
@klingdigital
Copy link

thanks bro!!!

@MathiasReker
Copy link

Nice script. Thanks for sharing. :-)
I would like to share a library I have coded with the same goal.

Example of usage:

<?php

use MathiasReker\PhpChmod\Scanner;

require __DIR__ . '/vendor/autoload.php';

(new Scanner())
    ->setDefaultFileMode(0644)
    ->setDefaultDirectoryMode(0755)
    ->setExcludedFileModes([0400, 0444, 0640])
    ->setExcludedDirectoryModes([0750])
    ->scan([__DIR__])
    ->fix();

Full documentation: https://github.com/MathiasReker/php-chmod

@klingdigital
Copy link

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment