On this page
wp_delete_file_from_directory( string $file, string $directory ): bool
Deletes a file if its path is within the given directory.
Parameters
$filestring Required-
Absolute path to the file to delete.
$directorystring Required-
Absolute path to a directory.
Return
bool True on success, false on failure.
Source
File: wp-includes/functions.php. View all references
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
Uses
| Uses | Description |
|---|---|
| wp_delete_file() wp-includes/functions.php | Deletes a file. |
| wp_is_stream() wp-includes/functions.php | Tests if a given path is a stream URL |
| wp_normalize_path() wp-includes/functions.php | Normalizes a filesystem path. |
| trailingslashit() wp-includes/formatting.php | Appends a trailing slash. |
Used By
| Used By | Description |
|---|---|
| wp_delete_attachment_files() wp-includes/post.php | Deletes all files that belong to the given attachment. |
Changelog
| Version | Description |
|---|---|
| 4.9.7 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_delete_file_from_directory