Automattic\WooCommerce\Internal\Utilities
FilesystemUtil::mkdir_p_not_indexable
Recursively creates a directory (if it doesn't exist) and adds an empty index.html and a .htaccess to prevent directory listing.
Method of the class: FilesystemUtil{}
No Hooks.
Returns
null. Nothing (null).
Usage
$result = FilesystemUtil::mkdir_p_not_indexable( $path, $allow_file_access ): void;
- $path(string) (required)
- Directory to create.
- $allow_file_access(true|false)
- Whether to allow file access while preventing directory listing.
Default:false (deny all access)
Changelog
| Since 9.3.0 | Introduced. |
FilesystemUtil::mkdir_p_not_indexable() FilesystemUtil::mkdir p not indexable code WC 10.8.1
public static function mkdir_p_not_indexable( string $path, bool $allow_file_access = false ): void {
$wp_fs = self::get_wp_filesystem();
if ( $wp_fs->is_dir( $path ) ) {
return;
}
if ( ! wp_mkdir_p( $path ) ) {
throw new \Exception( esc_html( sprintf( 'Could not create directory: %s.', wp_basename( $path ) ) ) );
}
$htaccess_content = $allow_file_access ? 'Options -Indexes' : 'deny from all';
$files = array(
'.htaccess' => $htaccess_content,
'index.html' => '',
);
foreach ( $files as $name => $content ) {
$wp_fs->put_contents( trailingslashit( $path ) . $name, $content );
}
}