WordPress at a glance

wp_delete_file_from_directory() WP 1.0

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

Works based on: wp_delete_file()

No Hooks.

Return

true/false. True on success, false on failure.

Usage

wp_delete_file_from_directory( $file, $directory );
$file(string) (required)
Absolute path to the file to delete.
$directory(string) (required)
Absolute path to a directory.

Changelog

Since 4.9.7 Introduced.

Code of wp delete file from directory: wp-includes/functions.php WP 5.2.2

<?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;
}

Related Functions

From tag: File system (create delete files folders)

No comments
    Hello, !     Log In . Register