我想将图像上传到我在插件中创建的名为 upload 的文件夹中。我在用着
<input type="file" name="upload_captcha_background" id="upload_captcha_background" />
请建议我如何在 uplaod 文件夹中上传图像。
如果您是从插件上传,那么您需要查看 wp_handle_upload如果您想更改上传文件夹的目录,那么您应该查看本教程:Smarter handling of WordPress plugin uploads基本上 wp_handle_upload 使用从wp upload dir返回的路径,您可以按照我链接的教程覆盖该路径。希望能帮助到你
编辑:
好的,这是一些示例代码:
if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
$uploadedfile = $_FILES['upload_captcha_background'];
$upload_overrides = array( 'test_form' => false );
add_filter('upload_dir', 'my_upload_dir');
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
remove_filter('upload_dir', 'my_upload_dir');
if ( $movefile ) {
echo "File is valid, and was successfully uploaded.\n";
var_dump( $movefile);
} else {
echo "Possible file upload attack!\n";
}
function my_upload_dir($upload) {
$upload['subdir'] = '/sub-dir-to-use' . $upload['subdir'];
$upload['path'] = $upload['basedir'] . $upload['subdir'];
$upload['url'] = $upload['baseurl'] . $upload['subdir'];
return $upload;
}
在代码文件中,添加以下行
$attachment_id = media_handle_upload('upload_captcha_background', $post->ID);
形式:
<input type="file" name="upload_captcha_background" id="upload_captcha_background" />
有关详细信息media_handle_upload()
,请查看文档。