Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to get the full local filename of an uploaded image so that I can copy it to a new directory and process it with my own image processing scripts. Which hooks/actions should I look into to achieve this?

Thanks

share|improve this question

2 Answers 2

up vote 4 down vote accepted

The handle_upload hook is called after wp_handle_upload is run, and it sends in a named array consisting of 'file', 'url' & 'type'. You could possibly use code like this to utilise it, depending on what you need to achieve:

function process_images($results) {
    if( $results['type'] === 'image/jpeg' ) { // or /png or /tif / /whatever
        $filename = $results[ 'file' ];
        $url = $results[ 'url' ];

        // manipulate the image
    }
}

add_action('wp_handle_upload', 'process_images');

Edit: If you need the attachment ID as well, it might be better hooking in at a higher level, such as add / edit attachment:

add_action('add_attachment', 'process_images');
add_action('edit_attachment', 'process_images');

in which case the variable sent in is the attachment_id, from which you can derive the rest using something like:

$metadata = wp_get_attachment_metadata( $results );
share|improve this answer
    
One quick question, how can I get the attachment ID from this also, as it is not included in $results? Many thanks. –  Tom Jan 11 '12 at 13:35
    
edited post accordingly –  Simon Scarfe Jan 11 '12 at 14:29

If you want the filename, and not just the path relative to the root of the file, you must add this line of code:

$filename = substr($results['file'], ( strrpos($results['file'], '/', -1) + 1 ) ); //add +1 to remove the slash at the beginning 

EDIT: Just after posting this strrpos() solution, I found a best one:

$filename = basename($results['url']);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.