wp_delete_file_from_directory( string   $file , string   $directory ): bool

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

Parameters

$file string required
Absolute path to the file to delete.
$directory string required
Absolute path to a directory.

Return

bool True on success, false on failure.

Source

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

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

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

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

	return wp_delete_file( $file );
}

Changuelog

Versionen Description
4.9.7 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.