* Helper functions to work with filesystem, uploads and media files.
use WPForms\Helpers\File;
* Get WPForms upload root path (e.g. /wp-content/uploads/wpforms).
* As of 1.7.0, you can pass in your own value that matches the output of wp_upload_dir()
* in order to use this function inside of a filter without infinite looping.
* @return array WPForms upload root path (no trailing slash).
function wpforms_upload_dir() {
$upload_dir = wp_upload_dir();
if ( ! empty( $upload_dir['error'] ) ) {
return [ 'error' => $upload_dir['error'] ];
$basedir = wp_is_stream( $upload_dir['basedir'] ) ? $upload_dir['basedir'] : realpath( $upload_dir['basedir'] );
$wpforms_upload_root = trailingslashit( $basedir ) . 'wpforms';
* Allow developers to change a directory where cache and uploaded files will be stored.
* @param string $wpforms_upload_root WPForms upload root directory.
$custom_uploads_root = apply_filters( 'wpforms_upload_root', $wpforms_upload_root );
if ( is_dir( $custom_uploads_root ) && wp_is_writable( $custom_uploads_root ) ) {
$wpforms_upload_root = wp_is_stream( $custom_uploads_root )
: realpath( $custom_uploads_root );
'path' => $wpforms_upload_root,
'url' => trailingslashit( $upload_dir['baseurl'] ) . 'wpforms',
* Create index.html file in the specified directory if it doesn't exist.
* @param string $path Path to the directory.
* @return int|false Number of bytes that were written to the file, or false on failure.
function wpforms_create_index_html_file( $path ) {
if ( ! is_dir( $path ) || is_link( $path ) ) {
$index_file = wp_normalize_path( trailingslashit( $path ) . 'index.html' );
// Do nothing if index.html exists in the directory.
if ( file_exists( $index_file ) ) {
// Create empty index.html.
return file_put_contents( $index_file, '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions
* Create index.php file in the specified directory if it doesn't exist.
* @param string $path Path to the directory.
* @return int|false Number of bytes that were written to the file, or false on failure.
function wpforms_create_index_php_file( string $path ) {
if ( ! is_dir( $path ) || is_link( $path ) ) {
$index_file = wp_normalize_path( trailingslashit( $path ) . 'index.php' );
// Do nothing if index.php exists in the directory.
if ( file_exists( $index_file ) ) {
header( $_SERVER[\'SERVER_PROTOCOL\'] . \' 404 Not Found\' );
header( \'Status: 404 Not Found\' );
return file_put_contents( $index_file, $data ); // phpcs:ignore WordPress.WP.AlternativeFunctions
* Create .htaccess file in the WPForms upload directory.
* @return bool True when the .htaccess file exists, false on failure.
function wpforms_create_upload_dir_htaccess_file(): bool {
* Whether to create upload dir .htaccess file.
* @param bool $allow True or false.
if ( ! apply_filters( 'wpforms_create_upload_dir_htaccess_file', true ) ) {
$htaccess_file = File::get_upload_dir() . '.htaccess';
$cache_key = 'upload_htaccess_file';
if ( File::is_file_updated( $htaccess_file, $cache_key ) ) {
if ( ! function_exists( 'insert_with_markers' ) ) {
require_once ABSPATH . 'wp-admin/includes/misc.php';
* Filters upload dir .htaccess file content.
* @param bool $allow True or false.
$contents = apply_filters(
'wpforms_create_upload_dir_htaccess_file_content',
'# Disable PHP and Python scripts parsing.
SetHandler default-handler
RemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo
RemoveType .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo
<IfModule headers_module>
Header set X-Robots-Tag "noindex"
$created = insert_with_markers( $htaccess_file, 'WPForms', $contents );
File::save_file_updated_stat( $htaccess_file, $cache_key );
* Create .htaccess file in the WPForms cache directory.
* @return bool True when the .htaccess file exists, false on failure.
function wpforms_create_cache_dir_htaccess_file(): bool {
* Whether to create cache dir .htaccess file.
* @param bool $allow True or false.
if ( ! apply_filters( 'wpforms_create_cache_dir_htaccess_file', true ) ) {
$htaccess_file = File::get_cache_dir() . '.htaccess';
if ( File::is_file_updated( $htaccess_file, 'cache_htaccess_file' ) ) {
if ( ! function_exists( 'insert_with_markers' ) ) {
require_once ABSPATH . 'wp-admin/includes/misc.php';
* Filters cache dir .htaccess file content.
* @param bool $allow True or false.
$contents = apply_filters(
'wpforms_create_cache_dir_htaccess_file_content',
'# Disable access for any file in the cache dir.
<IfModule !authz_core_module>
<IfModule authz_core_module>
$created = insert_with_markers( $htaccess_file, 'WPForms', $contents );
File::save_file_updated_stat( $htaccess_file );
* Convert a file size provided, such as "2M", to bytes.
* @link http://stackoverflow.com/a/22500394
* @param string $size File size.
function wpforms_size_to_bytes( $size ) {
if ( is_numeric( $size ) ) {
$suffix = substr( $size, - 1 );
$value = substr( $size, 0, - 1 );
switch ( strtoupper( $suffix ) ) {
* Convert a file size provided, such as "2M", to bytes.
* @link http://stackoverflow.com/a/22500394
* @param bool $bytes Whether the value should be in bytes or formatted.
* @return false|string|int
function wpforms_max_upload( $bytes = false ) {
$max = wp_max_upload_size();
return size_format( $max );