我允许从前端上载选定的几种类型的文件,例如。拉链mp4。jpeg和。pdf。到目前为止,所有这些文件类型在附加到上传到的帖子时都没有问题。然而,升级到WordPress 4.0后,我无法上传具体内容。zip文件类型,尽管它可以使用媒体上载界面从后端很好地上载。请注意,其他文件类型.mp4 and .pdf files still attach with no issues.
下面给出了执行附件功能的完整代码。
if ( isset( $_POST[\'upload_attachments\'] ) && $_SERVER[\'REQUEST_METHOD\'] === \'POST\' && wp_verify_nonce($_POST[\'secure_upload\'], \'upload_attachments_nonce\')) {
//checking if upload is empty
//checking if universal filesize is valid
if ($_FILES) { //loop through multiple files.
$files = $_FILES[\'upload\'];
foreach ($files[\'name\'] as $key => $value) {
if ($files[\'name\'][$key]) {
$file = array(
\'name\' => $files[\'name\'][$key],
\'type\' => $files[\'type\'][$key],
\'tmp_name\' => $files[\'tmp_name\'][$key],
\'error\' => $files[\'error\'][$key],
\'size\' => $files[\'size\'][$key]
);
$uploaded_file_type = $files[\'type\'][$key];
$allowed_file_types = array(\'image/jpg\', \'image/jpeg\', \'image/png\', \'application/pdf\', \'application/zip\', \'video/mp4\');
$uploaded_file_size = $files[\'size\'][$key];
$size_in_kb = $uploaded_file_size / 1024;
$file_size_limit = 10000; // Your Filesize in KB
if(in_array($uploaded_file_type, $allowed_file_types)) {
if( $size_in_kb > $file_size_limit ) {
$upload_error .= \'Image files must be smaller than \'.$file_size_limit.\'KB\';
return;
} else {
$_FILES = array("upload" => $file);
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$post_id);
//return; this loop neds to run multiple times so no return here
}
}
} else { $upload_error .= \'Invalid File type\';
return;
}
}
}
}
header (\'Location: \' . $_SERVER[\'REQUEST_URI\']);//Post, redirect and get
exit();
}//end of nonce check
<小时>
Helper function insert_attachment
作者:goldenapples
function insert_attachment($file_handler,$post_id,$setthumb=\'false\') {
// check to make sure its a successful upload
if ($_FILES[$file_handler][\'error\'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');
$attach_id = media_handle_upload( $file_handler, $post_id );
//if ($setthumb) update_post_meta($post_id,\'_thumbnail_id\',$attach_id);
return $attach_id;
}
<小时>
Form Html
<form method="post" id="frontend-attachment-upload-form" action="" enctype="multipart/form-data" >
<input type="file" multiple="true" name="upload[]">
<?php wp_nonce_field(\'upload_attachments_nonce\',\'secure_upload\'); ?>
<input type="submit" id="upload_attachments_button" name="upload_attachments" value="UPLOAD">
<form>
。zip文件根本并没有上传,否则我会在媒体库中找到它们,即使并没有附加。当我尝试上载时。在前端的zip文件中,它只返回错误“无效文件类型”。正如您所看到的,这一行清楚地定义了所有文件类型:
$allowed_file_types = array(\'image/jpg\', \'image/jpeg\', \'image/png\', \'application/pdf\', \'application/zip\', \'video/mp4\');
那么,为什么会出现。mp4。pdf。jpeg所有其他文件类型都可以很好地附加。zip没有?从逻辑上讲,如果循环中出现错误,则根本不会上载任何文件类型。事实也是如此。直到几天前,zip文件还可以正常上传。我对正在发生的事一无所知。请给我一点帮助。
以防万一,我检查了允许的文件类型和it列表。允许的zip文件类型。那么,wordpress是否可能在image.php
或file.php
或media.php
这会阻止在前端上载特定类型的文件吗?
<?php $allowed_mimes = get_allowed_mime_types();
echo \'<pre>\';
print_r($allowed_mimes);
echo \'</pre>\';
?>
<小时>
UPDATE : This issue has been solved, kindly take a look at the accepted answer.