How to rename an image attachment filename using php

I have the ID of the image uploaded and I need to rename it using php.

(The reasons because I have to do it are difficult to explain and I think they are not even necessary for the purposes of this discussion)

I have 4 problems to solve:

  1. Rename the main file.jpg and the related file-WIDTH-HEIGHT.jpg files – simply solved with rename php function
  2. Re-assign the new filename to the attachment so that WordPress knows the new filename (can I do it updating the ‘guid’ column of the attachment in the db?)
  3. Manage the related files (file-WIDTH-HEIGHT.jpg). Where are stored the relationship between the main file and the related ones?
  4. Update all these on posts/widget/ecc. (can I do it with a ‘simple’ string replace procedure on the sql file exported from the database?)

As you can see I have a bit of confusion in my mind because I don’t know completely the way used by WordPress to manage the attachments from a system/db point of view.

Thanks for any suggestions.

1 Answer
1

Let’s go and see. If I take arbitrary attachment in my dev install and dump its post object and custom fields I get the following:

object WP_Post (24) {
    public ID -> integer 1687
    public post_author -> string (1) "1"
    public post_date -> string (19) "2013-09-18 14:37:07"
    public post_date_gmt -> string (19) "2013-09-18 21:37:07"
    public post_content -> string (0) ""
    public post_title -> string (24) "dsc20050604_133440_34211"
    public post_excerpt -> string (0) ""
    public post_status -> string (7) "inherit"
    public comment_status -> string (4) "open"
    public ping_status -> string (6) "closed"
    public post_password -> string (0) ""
    public post_name -> string (24) "dsc20050604_133440_34211"
    public to_ping -> string (0) ""
    public pinged -> string (0) ""
    public post_modified -> string (19) "2013-09-18 14:37:07"
    public post_modified_gmt -> string (19) "2013-09-18 21:37:07"
    public post_content_filtered -> string (0) ""
    public post_parent -> integer 0
    public guid -> string (76) "http://dev.rarst.net/wp-content/uploads/2013/09/dsc20050604_133440_34211.jpg"
    public menu_order -> integer 0
    public post_type -> string (10) "attachment"
    public post_mime_type -> string (10) "image/jpeg"
    public comment_count -> string (1) "0"
    public filter -> string (3) "raw"
}

array(2) [
    '_wp_attached_file' => array(1) [
        string (36) "2013/09/dsc20050604_133440_34211.jpg"
    ]
    '_wp_attachment_metadata' => array(1) [
        string (687) "a:5:{s:5:"width";i:640;s:6:"height";i:480;s:4:"file";s:36:"2013/09/dsc20050604_133440_34211.jpg";s:5:"sizes";a:2:{s:9:"thumbnail";a:4:{s:4:"file";s:36:"dsc20050604_133440_34211-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:6:"medium";a:4:{s:4:"file";s:36:"dsc20050604_133440_34211-300x225.jpg";s:5:"width";i:300;s:6:"height";i:225;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:10:{s:8:"aperture";d:2;s:6:"credit";s:0:"";s:6:"camera";s:9:"CYBERSHOT";s:7:"caption";s:0:"";s:17:"created_timestamp";i:1117892080;s:9:"copyright";s:0:"";s:12:"focal_length";s:3:"9.7";s:3:"iso";s:3:"100";s:13:"shutter_speed";s:6:"0.0125";s:5:"title";s:0:"";}}"
    ]
]

The takeaways are respectively to your questions:

  1. The guid field does not matter, it holds a copy of calculated URL (yet it’s not supposed to be reliable URL in WP context), most of the time it should be ignored.
  2. The relationship between main file and sizes is stored in custom meta fields.
  3. Simple replace will not work, it will ruin serialized string in _wp_attachment_metadata meta field. You need serialize–aware search/replace tool, there are quite a few around (WP CLI, Search Replace DB, etc).

Leave a Comment