wp_delete_file_from_directory( string $file, string $directory ): bool
- Since
- 4.9.7
- Source
wp-includes/functions.php:7748
Compatibility
- WordPress
- since 4.9.7
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$filestring- Absolute path to the file to delete.
$directorystring- Absolute path to a directory.
Return value
bool- True on success, false on failure.
Performance profile
How much work a call to wp_delete_file_from_directory() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 16–45
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 49.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below wp_delete_file_from_directory()
What one call costs · 18 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_delete_file_from_directory() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
wp_is_stream() && $real_file === false && $real_directory === false | 16–18 | wp_is_stream() |
wp_is_stream() | 20–22 | wp_is_stream(), wp_normalize_path() |
wp_is_stream() && !$real_file | 23 | wp_is_stream(), trailingslashit() |
wp_is_stream() && $real_file !== false && $real_directory !== false | 24–26 | wp_is_stream(), wp_normalize_path(), wp_normalize_path() |
wp_is_stream() && $real_file | 26 | wp_is_stream(), trailingslashit(), wp_delete_file() |
wp_is_stream() && $real_directory !== false && $real_file !== false && !$real_file | 27 | wp_is_stream(), wp_normalize_path(), trailingslashit() |
!wp_is_stream() && $real_file === false && $real_directory === false | 27–29 | wp_is_stream(), wp_normalize_path(), realpath(), wp_normalize_path(), realpath() |
wp_is_stream() && $real_directory !== false && $real_file !== false && $real_file | 30 | wp_is_stream(), wp_normalize_path(), trailingslashit(), wp_delete_file() |
wp_is_stream() && $real_file !== false && $real_directory !== false && !$real_file | 31 | wp_is_stream(), wp_normalize_path(), wp_normalize_path(), trailingslashit() |
!wp_is_stream() | 31–33 | wp_is_stream(), wp_normalize_path(), realpath(), wp_normalize_path(), realpath(), wp_normalize_path() |
wp_is_stream() && $real_file !== false && $real_directory !== false && $real_file | 34 | wp_is_stream(), wp_normalize_path(), wp_normalize_path(), trailingslashit(), wp_delete_file() |
!wp_is_stream() && !$real_file | 34 | wp_is_stream(), wp_normalize_path(), realpath(), wp_normalize_path(), realpath(), trailingslashit() |
6 further outcomes, up to 45 instructions
!wp_is_stream() && $real_file !== false && $real_directory !== false | 35–37 | wp_is_stream(), wp_normalize_path(), realpath(), wp_normalize_path(), realpath(), wp_normalize_path(), wp_normalize_path() |
!wp_is_stream() && $real_file | 37 | wp_is_stream(), wp_normalize_path(), realpath(), wp_normalize_path(), realpath(), trailingslashit(), wp_delete_file() |
!wp_is_stream() && $real_directory !== false && $real_file !== false && !$real_file | 38 | wp_is_stream(), wp_normalize_path(), realpath(), wp_normalize_path(), realpath(), wp_normalize_path(), trailingslashit() |
!wp_is_stream() && $real_directory !== false && $real_file !== false && $real_file | 41 | wp_is_stream(), wp_normalize_path(), realpath(), wp_normalize_path(), realpath(), wp_normalize_path(), trailingslashit(), wp_delete_file() |
!wp_is_stream() && $real_file !== false && $real_directory !== false && !$real_file | 42 | wp_is_stream(), wp_normalize_path(), realpath(), wp_normalize_path(), realpath(), wp_normalize_path(), wp_normalize_path(), trailingslashit() |
!wp_is_stream() && $real_file !== false && $real_directory !== false && $real_file | 45 | wp_is_stream(), wp_normalize_path(), realpath(), wp_normalize_path(), realpath(), wp_normalize_path(), wp_normalize_path(), trailingslashit(), wp_delete_file() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 49 | 16–45 | 6 | |
| 8.5 | 49 | 16–45 | 6 | |
| 8.4 | 49 | 16–45 | 6 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 52 | 16–48 | 6 | |
| 8.2 | 52 | 16–48 | 6 | |
| 8.1 | 52 | 16–48 | 6 | |
| 7.4 | 52 | 16–48 | 6 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 5
- wp_is_stream()Tests if a given path is a stream URL
- wp_normalize_path()Normalizes a filesystem path.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- trailingslashit()Appends a trailing slash.
- wp_delete_file()Deletes a file.
Used by · 1
- wp_delete_attachment_files()Deletes all files that belong to the given attachment.
Source code
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 || ! str_starts_with( $real_file, trailingslashit( $real_directory ) ) ) { return false; } return wp_delete_file( $file );}Changelog
Introduced in 4.9.7. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.