• Hi,

    I have an import plugin with an image filed in it. This field contains the relative filename which is already manually uploaded on the server (by the user) in, let’s say, the /wp-content/import/images/ directory.

    The goal is to pick up this file and move it to the /wp-content/uploads/ folder with the right date path in it. How can I do this automatically?

    I have tried two ways, but both don’t work.

    Way 1:

    $attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
    add_post_meta($post_id, '_thumbnail_id', $attach_id, true);
    $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    wp_update_attachment_metadata( $attach_id,  $attach_data );

    This works, but the image stays in the /import/images/ directory, which is not the solution since I want them in a /uploads/2011/11/ structure.

    Way 2:
    Use the media_handle_upload() function:
    $attach_id = media_handle_upload( $file_handler, $post_id );

    This seems to be a solution, but the problem with this one is that the media_handle_upload function uses the index of the $_FILES variable and since, I’m not directly uploading pictures, this is not a working solution.

    Anyone a solution or ideas for this?

    Thanks,
    Arjan

Viewing 6 replies - 1 through 6 (of 6 total)
  • You almost had it 🙂 If you use:

    $attached_id = media_handle_sideload( $file_array, $post_id );

    it should work just fine.

    Thread Starter acsnaterse

    (@acsnaterse)

    Thanks, that’s indeed the solution I was looking for. For the ones who want to use this solution too: the $file_array is/must be similar to the array in $_FILES. For example:

    $filepath = '/relative/path/to/file.jpg';
    $wp_filetype = wp_check_filetype( basename( $filepath ), null );
    $aFile["name"] = basename( $filepath );
    $aFile["type"] = $wp_filetype;
    $afile["tmp_name"] = $filepath;
    
    $attach_id = $this->media_handle_sideload( $aFile, $post_id, 'This is optional file title' );

    The only problem, though, is that the media_handle_sideload() function contains a bug and therefore the $attach_id is not the ID of the wp_posts entry (which it should be), but the URL string of the uploaded file.

    It’s not a show stopper, everything works, but when you need this ID, for example for defining post thumbnails, it’s quite annoying.

    However, this bug will be fixed in WP3.1. See http://core.trac.wordpress.org/changeset/16383

    I’m having real problems getting media_handle_sideload to work. Does the filepath have to be relative? Surely it can also handle an absolute path?

    I’m trying to copy images from one website to another (upgrading a joomla site to a WP site while the joomla site stays live) and have extracted the URL’s to the images from the img tags using a Simple_Html_DOM parser.

    I think I should be able to feed these URL’s to media_handle_sideload and the function handles the image file transfer… but it won’t work!! I keep getting “[upload error] file is empty, try uploading something more substantial”.

    I’ve tried duplicating acsnaterse’s code but that doesn’t seem to change things.

    Many thanks for any suggestions.

    Here’s my code that’s failing dismally… any suggestions?

    // in the WP loop
    
    $post_ID = get_the_ID();
    $content = get_the_content();
    image_transfer($content, $post_ID);
    
    // in my functions.php
    
    function image_transfer($string, $post_id){
    
    // use simple_html_dom to parse string and extract image url from img src
    $html = str_get_html($string);
    foreach($html->find('img') as $element) {
    
    // copied from acsnaterse code above
    $filepath = $element->src;
    $wp_filetype = wp_check_filetype( basename( $filepath ), null );
    $aFile["name"] = basename( $filepath );
    $aFile["type"] = $wp_filetype;
    $afile["tmp_name"] = $filepath;
    
    $attach_id = media_handle_sideload( $aFile, $post_id );
    
    // see what's going on...
    print_r($wp_filetype);
    echo '<br/><br/>';
    print_r($attach_id);
    
    }

    Thanks.

    PHP is case sensitive.

    $afile[“tmp_name”] should be $aFile[“tmp_name”]

    Doesn’t solve the directory issue, though.

    Sorry, but I don’t understand what code actually moves the files to the media directory.
    Do you mean adding them to the library?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Automatically move uploaded files to uploads directory’ is closed to new replies.