使用 php 在服务器上上传 doc、docx、pdf 文件

upload doc, docx, pdf files on server using php

我希望在服务器上上传扩展名为 doc、docx 和 pdf 的文档。为此,我使用了以下代码,但无法上传文件

<?php
if($_POST['save'])
    {

        $target_dir = "reqdoc/";
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $uploadOk = 1;
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
        if (file_exists($target_file)) 
            {
                echo "Sorry, file already exists.";
                $uploadOk = 0;
            }
        if ($_FILES["fileToUpload"]["size"] > 500000)
            {
                echo "Sorry, your file is too large.";
                $uploadOk = 0;
            }
        if($imageFileType != "doc" && $imageFileType != "docx" && $imageFileType != "pdf" ) 
            {
                echo "Sorry, only doc, docx, pdf";
                $uploadOk = 0;
            }
        if ($uploadOk == 0) 
            {
                echo "Sorry, your file was not uploaded.";
            } 
        else 
            {
                if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
                    {
                        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
                    } 
                else 
                    {
                        echo "Sorry, there was an error uploading your file.";
                    }
            }
    }
?>

<p>&nbsp;</p>
<form class="form-horizontal" role="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
    <input type="file" name="fileToUpload" id="fileToUpload">
    <button type="submit" value="submit" name="save">Save</button>
</form>

我收到错误提示

Sorry, file already exists.Sorry, only doc, docx, pdfSorry, your file was not uploaded.

谁能告诉我代码哪里出错了

您忘记在表单标签中添加 enctype="multipart/form-data"

试试这个....

你错过了'enctype="multipart/form-data"'你的表格

您正在编写客户端代码,您只需要知道当您的表单包含任何元素时使用 multipart/form-data。

http://www.faqs.org/rfcs/rfc1867.html

<?php
if($_POST['save'])
    {

        $target_dir = "media/";
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $uploadOk = 1;
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
        if (file_exists($target_file)) 
            {
                echo "Sorry, file already exists.";
                $uploadOk = 0;
            }
        if ($_FILES["fileToUpload"]["size"] > 500000)
            {
                echo "Sorry, your file is too large.";
                $uploadOk = 0;
            }
        if($imageFileType != "doc" && $imageFileType != "docx" && $imageFileType != "pdf" ) 
            {
                echo "Sorry, only doc, docx, pdf";
                $uploadOk = 0;
            }
        if ($uploadOk == 0) 
            {
                echo "Sorry, your file was not uploaded.";
            } 
        else 
            {
                if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
                    {
                        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
                    } 
                else 
                    {
                        echo "Sorry, there was an error uploading your file.";
                    }
            }
    }
?>

<p>&nbsp;</p>
<form class="form-horizontal" enctype="multipart/form-data" role="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
    <input type="file" name="fileToUpload" id="fileToUpload">
    <button type="submit" value="submit" name="save">Save</button>
</form>