wppaste
WordPress

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
Returns the uploads subdirectory an attachment is stored in.

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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
7–38

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 41.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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.

WhenInstructionsCalls it makes
empty($uploads)7wp_get_upload_dir()
!empty($uploads) && $file_dir !== false && !$file_dir28wp_get_upload_dir(), wp_normalize_path(), untrailingslashit(), wp_normalize_path(), trailingslashit()
!empty($uploads) && $file_dir === false33wp_get_upload_dir(), wp_normalize_path(), untrailingslashit(), wp_normalize_path(), explode()
!empty($uploads) && $file_dir !== false && $file_dir38wp_get_upload_dir(), wp_normalize_path(), untrailingslashit(), wp_normalize_path(), trailingslashit(), explode()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev417–384
8.5417–384
8.4417–38411 fewer instructions than PHP 8.3
8.3527–494
8.2527–494
8.1527–494
7.4527–494

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

Used by · 1

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.