请在主题函数中添加以下代码。php文件:
add_filter( \'wp_check_filetype_and_ext\', \'file_and_ext_allow_for_user\', 10, 4 );
function file_and_ext_allow_for_user( $types, $file, $filename, $mimes )
{
if( false !== strpos( $filename, \'.doc\' ) ) {
$types[\'ext\'] = \'doc\';
$types[\'type\'] = \'application/msword\';
} else if( false !== strpos( $filename, \'.pdf\' ) ) {
$types[\'ext\'] = \'pdf\';
$types[\'type\'] = \'application/pdf\';
} else if( false !== strpos( $filename, \'.docx\' ) ) {
$types[\'ext\'] = \'docx\';
$types[\'type\'] = \'application/docx\';
}
return $types;
}
File Upload Code
if ( ! function_exists( \'wp_handle_upload\' ) ) {
require_once( ABSPATH . \'wp-admin/includes/file.php\' );
}
$uploadedfile = $_FILES[\'amfile\'];
if( $uploadedfile ){
$u = "Uploaded file set";
$upload_overrides = array( \'test_form\' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile && !isset( $movefile[\'error\'] ) ) {
$m = "Movefile file set";
echo "File is valid, and was successfully uploaded.\\n";
var_dump( $movefile);
} else {
$m = "Movefile file NOT set";
/**
* Error generated by _wp_handle_upload()
* @see _wp_handle_upload() in wp-admin/includes/file.php
*/
print_r($movefile);
$error = $movefile[\'error\'];
}
} else {
$u = "Uploaded file NOT set";
}
echo $u . "<br>";
echo $m . "<br>";
echo $error . "<br>";