如何在wp_handle_upload中更改上传目录


9

我正在尝试找出如何将wp_handle_upload函数用于自定义插件,以便可以指定自己的上传目录。到目前为止,该代码从我的插件设置页面中提取了一个文件,并与年和月一起上传到文件夹uploads文件夹。

我遇到了我认为可能有一些提示的链接-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>";
}

如何上传到我选择的目录?

任何帮助,不胜感激的家伙。

Answers:


5

这是我们如何在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 );

请注意,我们仅在从“下载”自定义帖子类型页面上传文件时修改上传目录。您需要针对插件的设置页面进行调整。
Pippin

它缺少“]”$upload['url'
Mario Radomanana

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.