PHP .doc文件上传不起作用

时间:2016-09-20 17:44:57

标签: php html html5 apache

我有一个页面,我希望用户能够上传.pdf,.docx和.doc文件。 PDF和DOCX上传工作正常,但不允许DOC文件。

这是我的表单和输入按钮:

<form action="?action=uploadForm" method="post" enctype="multipart/form-data" style="border:1px solid black;">
<input type="file" class="btn btn-md btn-primary btn-block" name="fileToUpload" id="fileToUpload" accept=".pdf,.docx,.doc" title="PDF and Word files only" />

在DOC文件中似乎永远不会将文件放入$_FILES

这是我的所有文件验证/上传逻辑:

if (isset($_GET['action']) == 'uploadForm') {
    // Get the option from the dropdown, so we can upload the form to the right directory
    $selectedTypeOfForm = $_POST['typeOfForm'];

    $target_dir = "files/" . $selectedTypeOfForm . "/";               // The directory for the upload to go to
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);   // The name of the file being uploaded
    $fileName = basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $fileType = pathinfo($target_file,PATHINFO_EXTENSION);

    dump($_FILES);

    $_SESSION['message'] .= "<br>";

    // Check if file already exists
    if (file_exists($target_file)) {
        $_SESSION['message'] .= "Sorry, the file <b>" . $fileName . "</b> already exists.<br>";
        $uploadOk = 0;
    }

    // Check file size
    if (($fileType == "pdf" && $_FILES["fileToUpload"]["size"] > MAXIMUM_PDF_SIZE) || ($fileType == "doc" && $_FILES["fileToUpload"]["size"] > MAXIMUM_WORD_SIZE) || ($fileType == "docx" && $_FILES["fileToUpload"]["size"] > MAXIMUM_WORD_SIZE)) {
        $_SESSION['message'] .= "Sorry, the file <b>" . $fileName . "</b> is too large. The file must be under ";

        if ($fileType == "pdf") {
            $_SESSION['message'] .= (MAXIMUM_PDF_SIZE / 1000000) . "MB for a PDF.<br>";
        } else {
            $_SESSION['message'] .= (MAXIMUM_WORD_SIZE / 1000000) . "MB for a Word document.<br>";
        }

        $uploadOk = 0;
    }

    // Allow certain file formats
    if($fileType != "pdf" && $fileType != "doc" && $fileType != "docx") {
        $_SESSION['message'] .= "Sorry, only PDF, DOC, and DOCX files are allowed. You tried to upload a <b>" . strtoupper($fileType) . "</b>.<br>";
        $uploadOk = 0;
    }

    // FINAL UPLOAD
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        $_SESSION['message'] .= "<br>Sorry, your file <b>" . $fileName . "</b> was not uploaded.<br>";
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            $_SESSION['message'] .= "<br>The file <b>" . basename($_FILES["fileToUpload"]["name"]) . "</b> has been uploaded.<br>";
        } else {
            $_SESSION['message'] .= "<br>Sorry, there was an error uploading the file <b>" . $fileName . "</b>.<br>";
        }
    }

    // header('Location:/forms.php');
    echo "<a href='/forms.php'>FORMS</a>";
    exit;
}

上传.DOC文件会显示以下消息:

Sorry, the file already exists.
Sorry, only PDF, DOC, and DOCX files are allowed. You tried to upload a .

Sorry, your file was not uploaded.

1 个答案:

答案 0 :(得分:0)

问题出在php.ini中的@RequestMapping(value = "/data", method = RequestMethod.GET) public File homePage(Map model) throws IOException { List<ForRent> allRentals = new ArrayList(); allRentals = forRentDao.allRentals(); JSONObject responseDetailsJson = new JSONObject(); JSONArray array = new JSONArray(); for (ForRent f : allRentals) { array.add(f.getLat()); array.add(f.getLon()); } responseDetailsJson.put("data", (Object) array);//Here you can see the data in json format File file = new File("/home/brennan/_repos/rent/src/main/webapp/json/data.json"); try { // Writing to a file file.createNewFile(); FileWriter fileWriter = new FileWriter(file); fileWriter.write(responseDetailsJson.toJSONString()); fileWriter.flush(); fileWriter.close(); } catch (IOException e) { } return file; upload_max_filesize。该文件在我的代码触及之前消失了,因为这些值设置为低于我尝试上传的.doc文件的大小。