_copy_image_file

Copy an existing image file.

Description

(string|false) _copy_image_file( (string) $attachment_id ); 

Returns (string|false)

New file path on success, false on failure.

Parameters (1)

0. $attachment_id (string)
The attachment id.

Usage

  1. if ( !function_exists( '_copy_image_file' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/image.php'; 
  3.  
  4. // The attachment id. 
  5. $attachment_id = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = _copy_image_file($attachment_id); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/image.php  
  1. function _copy_image_file( $attachment_id ) { 
  2. $dst_file = $src_file = get_attached_file( $attachment_id ); 
  3. if ( ! file_exists( $src_file ) ) 
  4. $src_file = _load_image_to_edit_path( $attachment_id ); 
  5.  
  6. if ( $src_file ) { 
  7. $dst_file = str_replace( basename( $dst_file ), 'copy-' . basename( $dst_file ), $dst_file ); 
  8. $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) ); 
  9.  
  10. /** 
  11. * The directory containing the original file may no longer 
  12. * exist when using a replication plugin. 
  13. */ 
  14. wp_mkdir_p( dirname( $dst_file ) ); 
  15.  
  16. if ( ! @copy( $src_file, $dst_file ) ) 
  17. $dst_file = false; 
  18. } else { 
  19. $dst_file = false; 
  20.  
  21. return $dst_file;