我正在尝试上载一个名为Products
使用wp_upload_bits
. 我只是在我的wp管理员中这样做。我不想将这些文件添加到媒体库中,我只想上传它们,并在显示产品时返回URL。
但是,当我这样做时,我得到一个错误,即它是从函数返回的无效文件类型。我已经在函数中使用它设置了enctype。php
//Allow file uploads
function update_edit_form() {
echo \' enctype="multipart/form-data"\';
} // end update_edit_form
add_action(\'post_edit_form_tag\', \'update_edit_form\');
这是我在“管理”菜单上生成元数据库的代码
function products_pdf_uploads_show_meta() {
global $meta_box_pdf_uploads, $post, $prefix;
echo \'<table class="form-table">\';
echo \'<p class="description">Upload your PDFs here</p>\';
foreach ($meta_box_pdf_uploads[\'fields\'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field[\'id\'], true);
echo \'<tr>\',
\'<td>\',
//\'<input type="file" style="width: 700px;" name="\', $field[\'id\'], \'" id="\', $field[\'id\'], \'" value="\', $meta ? $meta : null, \'" />\'
\'<input type="file" style="width: 700px;" name="\', $field[\'id\'], \'" id="\', $field[\'id\'], \'" />\',
\'</td>\',
\'</tr>\';
}
echo \'</table>\';
}
add_meta_box($meta_box_pdf_uploads[\'id\'], $meta_box_pdf_uploads[\'title\'], \'products_pdf_uploads_show_meta\', $meta_box_pdf_uploads[\'page\'], $meta_box_pdf_uploads[\'context\'], $meta_box_pdf_uploads[\'priority\']);
这与最后一行无关,是吗?我打电话的地方
add_meta_box
并传递自定义post类型
PRODUCT
没有指定页面或帖子?尽管如此。
以下是我上载metabox PDF文件的代码:
//Upload PDF files
foreach ($meta_box_pdf_uploads[\'fields\'] as $pdf_field) {
//put file array into a variable
$pdf = $_FILES[$pdf_field[\'name\']];
//if array is set and there is no error
if(isset($pdf[\'error\']) && $pdf[\'error\'] > 0) {
//setup error handling based on error code
wp_die(\'Error uploading file: Error Number is \' . $pdf[\'error\']);
} else { //Passed
//setup file type allowed
$supported_file_type = array(\'application/pdf\');
//Get the file type
$uploaded_file_type = $pdf[\'type\'];
//check if file type is allowed
if(in_array($uploaded_file_type, $supported_file_type)) {
//upload file
$uploaded_file = wp_upload_bits($_FILES[$pdf_field[\'name\']], null, file_get_contents($_FILES[$pdf_field[\'tmp_name\']]));
print_r($uploaded_file);
wp_die(\'did it upload?\');
add_post_meta($post_id, $pdf_field[\'id\'], $uploaded_file);
update_post_meta($post_id, $pdf_field[\'id\'], $uploaded_file);
} else { // NOT A PDF
wp_die(\'The file you tried to upload was not a PDF\');
}
}
当我做
print_r($uploaded_file)
要查看函数返回的内容,我得到
Array ( [error] => Invalid file type )
我已经检查了我的wp-includes/functions.php
它显然允许get_allowed_mime_types()
作用
\'pdf\' => \'application/pdf\',
我错过什么了吗?
以下是var_dump($_FILES)
. 我目前有两个字段,当我测试时,我只选择第一个要上载的文件。
array(2) { ["aps_pdf1"]=> array(5) { ["name"]=> string(12) "1testpdf.pdf" ["type"]=> string(15) "application/pdf" ["tmp_name"]=> string(14) "/tmp/phpwdXK0T" ["error"]=> int(0) ["size"]=> int(8278) }
["aps_pdf2"]=> array(5) { ["name"]=> string(0) "" ["type"]=> string(0) "" ["tmp_name"]=> string(0) "" ["error"]=> int(4) ["size"]=> int(0) } }