如何在wp_Handle_Upload中更改上传目录

时间:2012-10-10 作者:Xis

我正在尝试解决如何为自定义插件使用wp\\u handle\\u 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>";
}

How can I upload to a directory of my choosing?

任何帮助都很感激。

2 个回复
SO网友:Pippin

下面是一个完整的示例,介绍了如何在轻松的数字下载中做到这一点:

/**
 * 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 );

SO网友:Rarst

我不记得在实践中确实这样做过,但大多数(全部?)上载路径处理完成wp_upload_dir() 这适用于upload_dir 筛选返回的信息。您应该尝试在代码运行期间对其进行过滤,并将路径调整到所需位置。

结束