On this page
_copy_image_file( int $attachment_id ): string|false
This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.
Copies an existing image file.
Parameters
$attachment_idint Required-
Attachment ID.
Return
string|false New file path on success, false on failure.
Source
File: wp-admin/includes/image.php. View all references
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;
}
Related
Uses
| Uses | Description |
|---|---|
| _load_image_to_edit_path() wp-admin/includes/image.php | Retrieves the path or URL of an attachment’s attached file. |
| wp_unique_filename() wp-includes/functions.php | Gets a filename that is sanitized and unique for the given directory. |
| wp_mkdir_p() wp-includes/functions.php | Recursive directory creation based on full path. |
| get_attached_file() wp-includes/post.php | Retrieves attached file path based on attachment ID. |
| wp_basename() wp-includes/formatting.php | i18n-friendly version of basename(). |
Used By
| Used By | Description |
|---|---|
| Custom_Image_Header::step_3() wp-admin/includes/class-custom-image-header.php | Display third step of custom header image page. |
Changelog
| Version | Description |
|---|---|
| 3.4.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/_copy_image_file