虽然在条件中指定了文件类型,但PHP文件上载不起作用

虽然在条件中指定了文件类型,但PHP文件上载不起作用,php,file-upload,Php,File Upload,当我尝试上载word文档(.docx)文件时,它会发出警告消息“抱歉,仅允许word、txt、rtf或pdf文件。抱歉,您的文件未上载。”尽管在if条件语句中我提到msword是其中一种文件类型 <?php $target_dir = "./blog/wp-content/uploads/resumes"; $target_file = $target_dir . basename($_FILES["uploadCV"]["name"]); $uploadOk = 1; $imageFil

当我尝试上载word文档(.docx)文件时,它会发出警告消息“抱歉,仅允许word、txt、rtf或pdf文件。抱歉,您的文件未上载。”尽管在if条件语句中我提到msword是其中一种文件类型

<?php
$target_dir = "./blog/wp-content/uploads/resumes";
$target_file = $target_dir . basename($_FILES["uploadCV"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check file size
if ($_FILES["uploadCV"]["size"] > 1024000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "application/vnd.openxmlformats-officedocument.wordprocessingml.document" && $imageFileType != "application/msword" && $imageFileType != "application/txt" && $imageFileType != "rtf;application/rtf" && $imageFileType != "application/pdf" && $imageFileType != "rtf;text/richtext" && $imageFileType != "rtf;text/richtext") {
    echo "Sorry, only word, txt, rtf or pdf files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["uploadCV"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["uploadCV"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

HTML代码

<form class="assessment-form-step-3" action="free-visa-assessment-form-process-3.php" method="post" enctype="multipart/form-data">
                    <h2>About your Profession</h2>
                    <div>
                        <input class="assessment-occupation" name="assessment-occupation" data-validation="required" data-validation="length" data-validation-length="min3" type="text" placeholder="Your occupation" data-validation-error-msg="Please enter your occupation">
                    </div>

                    <div>
                        <select class="assessment-highest-qualification" name="assessment-highest-qualification" data-validation="required" data-validation-error-msg="Please select your highest qualification">
                            <option value="">Select your highest level of qualification</option>
                            <option value="PhD">PhD</option>
                            <option value="Masters">Masters</option>
                            <option value="Bachelor">Bachelor</option>
                            <option value="Diploma">Diploma</option>
                            <option value="Certificate">Certificate</option>
                        </select>
                    </div>

                    <div>
                        <select class="assessment-experience" name="assessment-experience" data-validation="required" data-validation-error-msg="Please select appropriate type of visa that you're looking for">
                             <option value="">Select years of work experience</option>
                              <option value="Less than a year">Less than a year</option>
                              <option value="1 year">1 year</option>
                              <option value="2 years">2 years</option>
                              <option value="3 years">3 years</option>
                              <option value="4 years+">4 years+</option>
                        </select>
                    </div>

                    <div>
                       <label for="">Upload your resume (optional)</label>
                        <input type="file" class="uploadCV" name="uploadCV" id="uploadCV" accept=".doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/pdf,.pdf,text/plain,application/rtf">
                    </div>

                    <div>
                        <textarea name="assessment-comments" placeholder="Enter your comments" class="assessment-comments" cols="30" rows="10"></textarea>
                    </div>

                    <button class="full-width green proceed-to-thankyou">Submit &nbsp;&nbsp;&nbsp; <img style="width:22px;" src="img/lock-icon.png" alt=""></button>
                    <p></p>
                </form>

关于你的职业
选择您的最高资格级别
博士
大师
单身汉
文凭
证明书
选择工作年限
不到一年
一年
两年
三年
4年+
上传你的简历(可选)
提交

free-visa-assessment-form-process-3.php

<?php

session_start();

if (!$_POST['assessment-occupation'] || !$_POST['assessment-highest-qualification'] || !$_POST['assessment-experience']) {
    echo "<p>Please supply all of the data!</p>";
    exit;
}
else {
    require('db-connection.php');
    require('file-upload-script.php');
    try {  
        $stmt = $conn->prepare("UPDATE visa SET job_title= :occupation, Qualifications= :qualification, experience= :experience, file_path= :file_upload, comments= :comments WHERE id= :id");

        // escape variables for security
        $stmt->bindParam(':occupation', $_POST['assessment-occupation']);
        $stmt->bindParam(':qualification', $_POST['assessment-highest-qualification']);
        $stmt->bindParam(':experience', $_POST['assessment-experience']);
        $stmt->bindParam(':file_upload', $target_file);
        $stmt->bindParam(':comments', $_POST['assessment-comments']);
        $stmt->bindParam(':id', $_SESSION["regId"]);

        $stmt->execute();

    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}

实际上,
$imageFileType=pathinfo($target\u文件,pathinfo\u扩展名)将返回文件的扩展名。因此,如果您想允许txt、word、pdf或rtf文件,请尝试以下方法

  //define an array containing file types
  $allowedFileTypes = array("txt", "pdf", "rtf", "docx", "doc");
  //condition to check
  if(!in_array($imageFileType, $allowedFileTypes ))
  {
      echo "Sorry, only word, txt, rtf or pdf files are allowed.";
      $uploadOk = 0;
  }
编辑

您必须使用此代码获取扩展名

  $imageFileType = pathinfo($_FILES['uploadCV']['name'],PATHINFO_EXTENSION);

代码中有两个错误

1) 您正在将文件扩展名与MIME类型匹配

if($imageFileType != "application/vnd.openxmlformats-officedocument.wordprocessingml.document" && $imageFileType != "application/msword" && $imageFileType != "application/txt" && $imageFileType != "rtf;application/rtf" && $imageFileType != "application/pdf" && $imageFileType != "rtf;text/richtext" && $imageFileType != "rtf;text/richtext") {
2) 您正在将目标文件夹传递到
pathinfo()

您必须创建一个扩展数组并匹配您的数组

 $allowed =  array('word', 'txt', 'rtf', 'pdf','docx');
    $ext = pathinfo($_FILES["uploadCV"]["name"], PATHINFO_EXTENSION);//pass file name here
    if(!in_array($ext,$allowed) ) {
        echo "Sorry, only word, txt, rtf or pdf files are allowed.";
        $uploadOk = 0;
    }

确保您在
表单标签中添加了
enctype=“multipart/form data”>
,我仍然收到相同的警告消息。我是否错误配置了html
您是否已将此
enctype=“multipart/form data”>
添加到您刚刚添加的表单标记中,但仍会收到相同的警告消息。
打印($\u文件)的post值!1告诉我们。我仍然收到同样的警告信息。我是否错误配置了html<代码>
仍收到相同的警告消息。
 $allowed =  array('word', 'txt', 'rtf', 'pdf','docx');
    $ext = pathinfo($_FILES["uploadCV"]["name"], PATHINFO_EXTENSION);//pass file name here
    if(!in_array($ext,$allowed) ) {
        echo "Sorry, only word, txt, rtf or pdf files are allowed.";
        $uploadOk = 0;
    }