Last active
May 14, 2025 07:49
-
-
Save mehrshaddarzi/b768c7f56425de16a4a162aaa27df5cb to your computer and use it in GitHub Desktop.
Block Run PHP File in WordPress Storage Folder (Uploads)
This file contains hidden or 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
# Block executables | |
<FilesMatch "\.(php|phtml|php3|php4|php5|pl|py|jsp|asp|html|htm|shtml|sh|cgi|suspected)$"> | |
deny from all | |
</FilesMatch> |
This file contains hidden or 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 | |
add_action('generate_rewrite_rules', 'create_htaccess_uploads_dir'); | |
function create_htaccess_uploads_dir(): void | |
{ | |
$uploads_dir = wp_upload_dir(null, false); | |
$file = $uploads_dir['basedir'] . '/.htaccess'; | |
if (file_exists($file)) { | |
@unlink($file); | |
} | |
$content = '# Block executables' . "\n"; | |
$content .= '<FilesMatch "\.(php|phtml|php3|php4|php5|pl|py|jsp|asp|html|htm|shtml|sh|cgi|suspected)$">' . "\n"; | |
$content .= 'deny from all' . "\n"; | |
$content .= '</FilesMatch>'; | |
file_put_contents($file, $content); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
define('DISALLOW_FILE_EDIT', true);