将$字段附加到$ _FILES

时间:2016-03-15 18:23:51

标签: php file

如果我有以下关联数组:

$fields = [                                 //store empty fields
    'name' => $_POST['name'],
    'email' => $_POST['email'],     
    'phone' => $_POST['phone'],
    'location' => $_POST['location'],
    'proposal' => $_POST['proposal'] //--------------> proposal = attachment file
];

如何将它们用于$_FILES["attachment"]["name"]

我的计划是检查提案是pdf,docx还是.doc

这是我到目前为止所做的:

        if(!empty($_FILES[$fields['proposal']]['name']) ){
            //variables
            $fileName = $_FILES[$fields['proposal']]['name'];
            $tempName = $_FILES[$fields['proposal']]['tmp_name'];
            $fileType = $_FILES[$fields['proposal']]['type'];

            //get the extension of the file
            $base = basename($fileName);
            $extension = substr($base, strlen($base)-4, strlen($base));

            //only these file types will be allowed
            $allowedExtension = array(".doc", ".pdf", "docx");

            //check that this file type is allowed
            if (in_array($extension, $allowedExtension)) {


            }else {
            $errors['proposal'] = 'You can only upload pdf files';                  

            } 
        }

0 个答案:

没有答案