如何将上传的图像从表单添加到媒体库wordpress?

时间:2014-09-11 20:37:56

标签: php wordpress

我有一张表格。按下提交按钮后,图像会上传但不会显示在媒体库中。

如何将图像从表单添加到媒体库?

HTML

<form method="post" enctype="multipart/form-data">
<input type='file' name='file'>
<input type='submit' value='send'>
</form>

WordPress的

if (!function_exists('wp_handle_upload')) {
require_once( ABSPATH . 'wp-admin/includes/file.php' ); }

$uploaded_file = $_FILES['file'];

upload_overrides = array('test_form' => false);

$file = wp_handle_upload($uploaded_file,$upload_overrides);

if ($file) {
echo "File is valid and was successfully uploaded";
var_dump($file);
} else {
echo "Possible file upload attack";
}

我做过研究,http://codex.wordpress.org/Function_Reference/wp_insert_attachment的代码不起作用。

它需要正确的wp_insert_attachment()才能工作。谢谢。

2 个答案:

答案 0 :(得分:0)

注意你正在使用$ file变量并打印$ movefile.So please

replace var_dump($movefile); with var_dump($file);

我认为它会为你提供一些帮助

答案 1 :(得分:0)

您可能想要查看media_handle_upload()

$return = media_handle_upload('file', 0);

if (is_int($return)) {
    echo "File is valid and was successfully uploaded. New ID: $return";
} else {
    var_dump($return);
}