WordPress Version: .27
/**
* Deletes a file if its path is within the given directory.
*
* @since 4.9.7
*
* @param string $file Absolute path to the file to delete.
* @param string $directory Absolute path to a directory.
* @return bool True on success, false on failure.
*/
function wp_delete_file_from_directory($file, $directory)
{
$real_file = realpath(wp_normalize_path($file));
$real_directory = realpath(wp_normalize_path($directory));
if (false === $real_file || false === $real_directory || strpos(wp_normalize_path($real_file), trailingslashit(wp_normalize_path($real_directory))) !== 0) {
return false;
}
/** This filter is documented in wp-admin/custom-header.php */
$delete = apply_filters('wp_delete_file', $file);
if (!empty($delete)) {
@unlink($delete);
}
return true;
}