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've got a plugin that transfers some files over to the uploads folder of the site in which the plugin is installed.

It works fine, however, the images are not appearing in the Media Manager. I expect some database registration is involved.

Given the script below which copies the files into the directory, what command would I need to add to the loop to register each image for the media manager?

    foreach(new RecursiveIteratorIterator($rdi) as $files) 
    {
         if ($files->isFile()) 
         {
            $imagepath = $files->getRealPath();
            $image = basename($files->getPathname());
            copy($imagepath, $my_target_folder.'/'.$image);
         }
    }
share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

add this to your for each and $filename to each file,

 $wp_filetype = wp_check_filetype(basename($filename), null );
  $attachment = array(
     'post_mime_type' => $wp_filetype['type'],
     'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
     'post_content' => '',
     'post_status' => 'inherit'
  );
  $attach_id = wp_insert_attachment( $attachment, $filename, 0 );
  // you must first include the image.php file
  // for the function wp_generate_attachment_metadata() to work
  require_once(ABSPATH . "wp-admin" . '/includes/image.php');
  $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
  wp_update_attachment_metadata( $attach_id,  $attach_data );
share|improve this answer
    
Thanks! testing it now. –  Scott B Mar 23 '11 at 22:30
    
@Scott: let me know how it goes –  Bainternet Mar 23 '11 at 22:33
    
what value should I use for $filename? eg, would I use my $image variable or my $imagepath variable? ($image = test.png, $imagepath = "c:/host/sites/...") –  Scott B Mar 23 '11 at 22:35
    
@Scott: $filename - Location of the file on the server. Use absolute path and not the URI of the file. The file MUST be on the uploads directory. –  Bainternet Mar 23 '11 at 22:39
    
Got it. +1 for the excellent help. I really appreciate it. –  Scott B Mar 23 '11 at 22:46
add comment

Have you looked at the function media_handle_sideload()? It seems to be what you're looking for.

It works essentially the same way as media_handle_upload(), but takes a file thats already on the local server, moves it to the current uploads directory, and generates the post record necessary to add it to the Media Manager.

share|improve this answer
    
@GA: little nuggets like this is why i come to SA. I'd already selected the answer and you still thought to offer a second option. I sincerely love this place! +1 –  Scott B Mar 24 '11 at 2:19
    
@Scott B thanks - yeah, I try to answer anything that I know, whether an answer has been accepted or not. Someone else will always find this question searching for something, and its good to have these answers archived! –  goldenapples Mar 24 '11 at 5:53
    
@GA: There's not much info on this function. I did find a use-case example here > wordpress.org/support/topic/… - There appear to be some issues prior to a 3.1 related bug fix. I'm holding out hope of adopting this in replacement of @Bainternet's suggestion as it seems more appropriate. –  Scott B Mar 24 '11 at 17:20
    
Here's a link to the bug track issue > core.trac.wordpress.org/ticket/15432 –  Scott B Mar 24 '11 at 17:22
    
Interesting. It looks like that patch has been applied since 3.01, though... –  goldenapples Mar 24 '11 at 17:33
add comment

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.