_copy_image_file( int $attachment_id ): string|false
- Since
- 3.4.0
- Source
wp-admin/includes/image.php:1333
Compatibility
- WordPress
- since 3.4.0
- 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
$attachment_idint- Attachment ID.
Return value
string|false- New file path on success, false on failure.
Performance profile
How much work a call to _copy_image_file() 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
- Heavy
- Scaling
- Constant
- Instructions
- 13–48
- Plugin surface
- None
- Called by
- 1
Reads or writes the filesystem via file_exists().
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 50.
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
- filesystemfilesystem access
file_exists()called directly - hookthird-party callbacks
apply_filters()one call below _copy_image_file()
Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
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 _copy_image_file() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
file_exists() && !$dst_file | 13 | get_attached_file(), file_exists() |
!file_exists() && !$dst_file | 17 | get_attached_file(), file_exists(), _load_image_to_edit_path() |
file_exists() && $dst_file | 43–44 | get_attached_file(), file_exists(), wp_basename(), wp_basename(), wp_basename(), wp_unique_filename(), wp_mkdir_p(), copy() |
!file_exists() && $dst_file | 47–48 | get_attached_file(), file_exists(), _load_image_to_edit_path(), wp_basename(), wp_basename(), wp_basename(), wp_unique_filename(), wp_mkdir_p(), copy() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 50 | 13–48 | 3 | |
| 8.5 | 50 | 13–48 | 3 | |
| 8.4 | 50 | 13–48 | 3 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 59 | 13–57 | 3 | |
| 8.2 | 59 | 13–57 | 3 | |
| 8.1 | 59 | 13–57 | 3 | |
| 7.4 | 59 | 13–57 | 3 |
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
- get_attached_file()Retrieves attached file path based on attachment ID.
- _load_image_to_edit_path()Retrieves the path or URL of an attachment's attached file.
- wp_basename()i18n-friendly version of basename().
- wp_unique_filename()Gets a filename that is sanitized and unique for the given directory.
- wp_mkdir_p()Recursive directory creation based on full path.
Used by · 1
- Custom_Image_Header::step_3()Displays third step of custom header image page.
Source code
function _copy_image_file( $attachment_id ) { $dst_file = get_attached_file( $attachment_id ); $src_file = $dst_file; if ( ! file_exists( $src_file ) ) { $src_file = _load_image_to_edit_path( $attachment_id ); } if ( $src_file ) { $dst_file = str_replace( wp_basename( $dst_file ), 'copy-' . wp_basename( $dst_file ), $dst_file ); $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), wp_basename( $dst_file ) ); /* * The directory containing the original file may no longer * exist when using a replication plugin. */ wp_mkdir_p( dirname( $dst_file ) ); if ( ! copy( $src_file, $dst_file ) ) { $dst_file = false; } } else { $dst_file = false; } return $dst_file;}Changelog
Introduced in 3.4.0. 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 7.1.0 tag, from
src/wp-admin/includes/image.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.