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 am trying to work out how to use the wp_handle_upload function for a custom plugin so that i can specify my own own upload directory. The code so far takes a file from my plugin settings page and uploads to the folder uploads folder with the year and month.

I came across this link which I thought might have some clues - http://yoast.com/smarter-upload-handling-wp-plugins

if(strtolower($_SERVER['REQUEST_METHOD']) == "post"){

     $overrides = array('test_form' => false);
     $file = wp_handle_upload($_FILES['binaryFile'], $overrides);

     echo "<pre>" . print_r($file, true) . "</pre>";
}

How can I upload to a directory of my choosing?

Any help much appreciated guys.

share|improve this question
add comment

2 Answers

Here's a complete example for how we do it in Easy Digital Downloads:

/**
 * Set Upload Directory
 *
 * Sets the upload dir to edd. This function is called from
 * edd_change_downloads_upload_dir()
 *
 * @since 1.0
 * @return array Upload directory information
*/
function edd_set_upload_dir( $upload ) {
    $upload['subdir'] = '/edd' . $upload['subdir'];
    $upload['path'] = $upload['basedir'] . $upload['subdir'];
    $upload['url']  = $upload['baseurl'] . $upload['subdir'];
    return $upload;
}


/**
 * Change Downloads Upload Directory
 *
 * Hooks the edd_set_upload_dir filter when appropriate. This function works by
 * hooking on the WordPress Media Uploader and moving the uploading files that
 * are used for EDD to an edd directory under wp-content/uploads/ therefore,
 * the new directory is wp-content/uploads/edd/{year}/{month}. This directory
 * provides protection to anything uploaded to it.
 *
 * @since 1.0
 * @global $pagenow
 * @return void
 */
function edd_change_downloads_upload_dir() {
    global $pagenow;

    if ( ! empty( $_REQUEST['post_id'] ) && ( 'async-upload.php' == $pagenow || 'media-upload.php' == $pagenow ) ) {
        if ( 'download' == get_post_type( $_REQUEST['post_id'] ) ) {
            add_filter( 'upload_dir', 'edd_set_upload_dir' );
        }
    }
}
add_action( 'admin_init', 'edd_change_downloads_upload_dir', 999 );
share|improve this answer
    
Note, we only modify the upload directory when uploading files from our Downloads custom post type page. You will need to adjust it for your plugin's settings page. –  Pippin Jun 9 '13 at 3:37
    
It lacks "]" with $upload['url' –  Mario Johnathan Mar 25 at 9:08
    
Fixed the error, thanks. –  Pippin Mar 29 at 17:31
add comment

I don't remember doing exactly this in practice, but most (all?) upload path handling goes through wp_upload_dir() that applies upload_dir filter to returned information. You should try filtering it for the duration of your code running and adjusting paths to wanted location.

share|improve this answer
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.