Skip to content

Instantly share code, notes, and snippets.

@ashokmhrj
Created March 6, 2020 03:30
Show Gist options
  • Save ashokmhrj/9ff0ac46c7564b86d8a634b7969c6b24 to your computer and use it in GitHub Desktop.
Save ashokmhrj/9ff0ac46c7564b86d8a634b7969c6b24 to your computer and use it in GitHub Desktop.
setting file permission from parent folder permission
<?php function scan_folders( $path = '', $return = array() ) {
$path = $path == ''? dirname( __FILE__ ) : $path;
$lists = @scandir( $path );
if ( ! empty( $lists ) ) {
foreach ( $lists as $f ) {
if ( is_dir( $path . DIRECTORY_SEPARATOR . $f ) && $f != "." && $f != ".." ) {
if ( ! in_array( $path . DIRECTORY_SEPARATOR . $f, $return ) )
$return[] = trailingslashit( $path . DIRECTORY_SEPARATOR . $f );
scan_folders( $path . DIRECTORY_SEPARATOR . $f, $return);
}
}
}
return $return;
}
/**
* restrict direct view
*/
function create_htaccess($upload_path ){
$rules = "Options -Indexes\n";
$rules .= "deny from all\n";
$rules .= "<FilesMatch '\.(pdf)$'>\n";
$rules .= "Order Allow,Deny\n";
$rules .= "Allow from all\n";
$rules .= "</FilesMatch>\n";
if( ! file_exists( $upload_path.'/.htaccess' ) ) {
@file_put_contents( $upload_path . '/.htaccess', $rules );
}
// Top level blank index.php
if ( ! file_exists( $upload_path . '/index.php' ) && wp_is_writable( $upload_path ) ) {
@file_put_contents( $upload_path . '/index.php', '<?php' . PHP_EOL . '// Silence is golden.' );
}
}
/**
* create index file in folder
*/
function create_index( $upload_path ){
// Now place index.php files in all sub folders
$folders = scan_folders( $upload_path );
foreach ( $folders as $folder ) {
// Create index.php, if it doesn't exist
if ( ! file_exists( $folder . 'index.php' ) && wp_is_writable( $folder ) ) {
@file_put_contents( $folder . 'index.php', '<?php' . PHP_EOL . '// Silence is golden.' );
}
}
}
function file_permission( $file ) {
$target_parent = dirname( $file );
$stat = @stat( $target_parent );
$file_perms = 0755;
if( $stat ) {
$file_perms = $stat['mode'] & 0007755;
}
@chmod( $file, $file_perms );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment