WP_REST_Attachments_Controller::get_attachment_upload_subdir( string $attached_file ): string|null
- Since
- 7.1.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php:3204
Description
Used to place a sideloaded file alongside the attachment it extends. The result is concatenated into a filesystem path by the caller, so it is returned only when the attachment resolves inside the uploads directory and the stored path is well formed.
Compatibility
- WordPress
- since 7.1.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in 1 of the 5 tracked releases, added in 7.1.0, and compiles on PHP 7.4 through 8.6-dev.
Parameters
$attached_filestring- Absolute path to the attached file.
Return value
string|null- Subdirectory beginning with a slash, an empty string when the attachment sits in the base directory, or null when the attachment is not inside the uploads directory.
Performance profile
How much work a call to WP_REST_Attachments_Controller::get_attachment_upload_subdir() 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
- 7–38
- 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 41.
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 one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Attachments_Controller::get_attachment_upload_subdir() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($uploads) | 7 | wp_get_upload_dir() |
!empty($uploads) && $file_dir !== false && !$file_dir | 28 | wp_get_upload_dir(), wp_normalize_path(), untrailingslashit(), wp_normalize_path(), trailingslashit() |
!empty($uploads) && $file_dir === false | 33 | wp_get_upload_dir(), wp_normalize_path(), untrailingslashit(), wp_normalize_path(), explode() |
!empty($uploads) && $file_dir !== false && $file_dir | 38 | wp_get_upload_dir(), wp_normalize_path(), untrailingslashit(), wp_normalize_path(), trailingslashit(), explode() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 41 | 7–38 | 4 | |
| 8.5 | 41 | 7–38 | 4 | |
| 8.4 | 41 | 7–38 | 4 | 11 fewer instructions than PHP 8.3 |
| 8.3 | 52 | 7–49 | 4 | |
| 8.2 | 52 | 7–49 | 4 | |
| 8.1 | 52 | 7–49 | 4 | |
| 7.4 | 52 | 7–49 | 4 |
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_get_upload_dir()Retrieves uploads directory information.
- untrailingslashit()Removes trailing forward slashes and backslashes if they exist.
- 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.
Used by · 1
- WP_REST_Attachments_Controller::sideload_item()Side-loads a media file without creating a new attachment.
Source code
protected function get_attachment_upload_subdir( string $attached_file ): ?string { $uploads = wp_get_upload_dir(); if ( empty( $uploads['basedir'] ) ) { return null; } $basedir = untrailingslashit( wp_normalize_path( $uploads['basedir'] ) ); $file_dir = wp_normalize_path( dirname( $attached_file ) ); /* * The attachment's directory must be the uploads base directory itself * or a directory inside it. The trailing slash in the prefix comparison * keeps a sibling directory that merely shares the prefix (for example * 'uploads-elsewhere' next to 'uploads') from matching. */ if ( $file_dir !== $basedir && ! str_starts_with( $file_dir, trailingslashit( $basedir ) ) ) { return null; } $subdir = (string) substr( $file_dir, strlen( $basedir ) ); // A prefix match alone does not rule out a path that climbs back out. if ( in_array( '..', explode( '/', $subdir ), true ) ) { return null; } return $subdir; }Changelog
Introduced in 7.1.0.
Signature, return type and hooks compared across 1 parsed release.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.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.