[wp-hackers] Lookup attachment from file name/path

Dion Hulse (dd32) wordpress at dd32.id.au
Thu Apr 28 01:50:58 UTC 2011


This is going to vary depending on what exact data is corrupt/missing.
If it's the Image Metadata(postmeta), then that's the only thing that ties
an upload file to a attachment post - You might be able to reverse-engineer
it based on year&month of the attachment, and it's sanitized name..

If it's the above case, i'd do something akin to this:
foreach file in folders
   if is a resized filename; Delete it, continue.
   get month and year from folder names
   get sanitized filename from filename
   Search for attachments which match this; if none found, add it to the
manual list
   If a match was found, run the WordPress generate metadata code for that
Attachment ID and filename.. (Note: I'm not sure how it's going to handle
running on already existing attachments, etc) - That should re-generate the
thumbnails and add all the correct metadata
endforeach;

Of course.. The clients backup service is where this should come into play..
restore the Database.. If they don't have a backup service from their host,
consider setting up a database backup nightly to a off-server location, or
invest in a VaultPress account (or similar if there are any competing
services out yet)


On 28 April 2011 05:51, Paul <paul at codehooligans.com> wrote:

> I have a client who lost their site to a hack (a bad programmer not
> intruder). Somehow some of the DB references to many of the attachments in
> the /uploads/ folder are missing. Meaning for any given file under /uploads/
> some are  and some are not WP. Would rather not purge everything and reload
> the tree. Mainly because I would loose the parent association of the current
> one. And yes there are some 5k attachments in the uploads tree.
>
> I know there is a function to look up a post/page given the path. But
> wondering if anyone knows of a way to check if a given file is an
> attachment?
>
> I know I can load all attachments then check the attachment_url to the path
> of the file. But to do that for every file seems inefficient.
>
> P-
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list