【问题标题】:upload doc, docx, pdf files on server using php使用 php 在服务器上上传 doc、docx、pdf 文件
【发布时间】:2015-10-28 10:54:37
【问题描述】:

我希望在服务器上上传具有 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.

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

【问题讨论】:

  • 使用 if..elseif..else
  • enctype='multipart/formdata' 添加到您的表单中,这是一个名为MAX_FILE_SIZE 的隐藏字段,其大小(以字节为单位)作为值 - 这些应该会有所帮助

标签: php mysql file-upload upload


【解决方案1】:

试试这个....

您的表单中缺少 '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>

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-25
      • 1970-01-01
      • 2023-03-24
      • 2017-05-24
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多