wp_delete_file_from_directory() WordPress Function

The wp_delete_file_from_directory() function is used to delete a file from a specified directory. This function is typically used when cleaning up after a plugin or theme has been uninstalled.

wp_delete_file_from_directory( string $file, string $directory ) #

Deletes a file if its path is within the given directory.


Parameters

$file

(string)(Required)Absolute path to the file to delete.

$directory

(string)(Required)Absolute path to a directory.


Top ↑

Return

(bool) True on success, false on failure.


Top ↑

Source

File: wp-includes/functions.php

function wp_delete_file_from_directory( $file, $directory ) {
	if ( wp_is_stream( $file ) ) {
		$real_file      = $file;
		$real_directory = $directory;
	} else {
		$real_file      = realpath( wp_normalize_path( $file ) );
		$real_directory = realpath( wp_normalize_path( $directory ) );
	}

	if ( false !== $real_file ) {
		$real_file = wp_normalize_path( $real_file );
	}

	if ( false !== $real_directory ) {
		$real_directory = wp_normalize_path( $real_directory );
	}

	if ( false === $real_file || false === $real_directory || strpos( $real_file, trailingslashit( $real_directory ) ) !== 0 ) {
		return false;
	}

	wp_delete_file( $file );

	return true;
}


Top ↑

Changelog

Changelog
VersionDescription
4.9.7Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More