I am building a plugin which has a file upload field in it's settings page. The files should not be image files but should be .doc, .txt etc. Struggling with the code for it. Any help would be greatly appreciated !

Edit 1: Here is the code -

$options = get_option('nn_sub_general_options');
if(!isset($options['nn_file_uploader'])) $options['nn_file_uploader'] = '';
require_once( ABSPATH . 'wp-admin/includes/file.php' );
echo "<form method='post' action='options.php' enctype='multipart/form-data'>
<input type='file' name='nn_sub_general_options[nn_file_uploader]' id='nn_sub_general_options[nn_file_uploader]' /></form>";
$uploadedfile = $_FILES['nn_sub_general_options[nn_file_uploader]'];
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );

There seems to be some problem with the 3rd last line of the code as it gives me an Undefined index error there

  • 1
    An example of the code you have tried is very wellcome to help you in the best way.
    – cybmeta
    Commented Sep 16, 2013 at 14:05

2 Answers 2


First do a var_dump on the $_FILES variable to check if the array key you are using is the correct one, and you should never just grab a variable that comes from user input and just use it, try doing a isset or empty before you use the variable.

After you found what is the key you will use wp_check_filetype because it will give safer data about the file, you will have to do 2 in_array to check if both the mimetype of the file and the extension belongs to the set you are allowing the user to upload, if not throw the user an message. Exemple of in_array:

$extention = 'doc';
$allowed_extensions = array( 'doc', 'docx', 'pdf' );

$mime = 'text/javascript';
$allowed_mime = array( 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.ms-word.document.macroEnabled.12' );

if ( ! in_array( $extension, $allowed_extensions ) ){
    return false;

if ( ! in_array( $mime, $allowed_mime ) ){
    return false;

If you want to check for file size, you can use check_upload_size, which is also a WordPress method that uses the site upload limit as a guide.

$allowedExts = array("gif", "jpeg", "jpg", "png"); 
$temp = explode(".", $_FILES["file"]["name"]); 
$extension = end($temp); 
if (
        ( $_FILES["file"]["type"] == "image/gif") || 
        ($_FILES["file"]["type"] == "image/jpeg") || 
        ($_FILES["file"]["type"] == "image/jpg") || 
        ($_FILES["file"]["type"] == "image/pjpeg") || 
        ($_FILES["file"]["type"] == "image/x-png") || 
        ($_FILES["file"]["type"] == "image/png")
    && ($_FILES["file"]["size"] < 20000) 
    && in_array($extension, $allowedExts)

) {  


you can use this condition for your file type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.