• <bdo id='Iku0v'></bdo><ul id='Iku0v'></ul>
    1. <tfoot id='Iku0v'></tfoot>

      <small id='Iku0v'></small><noframes id='Iku0v'>

    2. <i id='Iku0v'><tr id='Iku0v'><dt id='Iku0v'><q id='Iku0v'><span id='Iku0v'><b id='Iku0v'><form id='Iku0v'><ins id='Iku0v'></ins><ul id='Iku0v'></ul><sub id='Iku0v'></sub></form><legend id='Iku0v'></legend><bdo id='Iku0v'><pre id='Iku0v'><center id='Iku0v'></center></pre></bdo></b><th id='Iku0v'></th></span></q></dt></tr></i><div id='Iku0v'><tfoot id='Iku0v'></tfoot><dl id='Iku0v'><fieldset id='Iku0v'></fieldset></dl></div>

      <legend id='Iku0v'><style id='Iku0v'><dir id='Iku0v'><q id='Iku0v'></q></dir></style></legend>

      HTML/PHP 文件上传

      HTML/PHP File Upload(HTML/PHP 文件上传)

        • <bdo id='LPEq7'></bdo><ul id='LPEq7'></ul>
          <legend id='LPEq7'><style id='LPEq7'><dir id='LPEq7'><q id='LPEq7'></q></dir></style></legend>

              <small id='LPEq7'></small><noframes id='LPEq7'>

              <tfoot id='LPEq7'></tfoot>
            1. <i id='LPEq7'><tr id='LPEq7'><dt id='LPEq7'><q id='LPEq7'><span id='LPEq7'><b id='LPEq7'><form id='LPEq7'><ins id='LPEq7'></ins><ul id='LPEq7'></ul><sub id='LPEq7'></sub></form><legend id='LPEq7'></legend><bdo id='LPEq7'><pre id='LPEq7'><center id='LPEq7'></center></pre></bdo></b><th id='LPEq7'></th></span></q></dt></tr></i><div id='LPEq7'><tfoot id='LPEq7'></tfoot><dl id='LPEq7'><fieldset id='LPEq7'></fieldset></dl></div>

                  <tbody id='LPEq7'></tbody>
                本文介绍了HTML/PHP 文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                所以我希望用户只能上传文档或 docx.所以首先这是我的html:

                So i want the user to only be able to upload docs or docx's. So first here's my html:

                <form action="upload_file.php" method="post" enctype="multipart/form-data">
                    Select a file: <input type="file" name="img">
                    <input type="submit">
                </form>
                

                这是我的 php:

                $allowedExts = array("doc", "docx");
                    $extension = end(explode(".", $_FILES["file"]["name"]));
                
                    if ($extension!=".doc" || $extension!=".doc"
                    && ($_FILES["file"]["size"] < 200000)
                    && in_array($extension, $allowedExts)) {
                        if ($_FILES["file"]["error"] > 0)
                        {
                            echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
                        }
                        else
                        {
                            echo "Upload: " . $_FILES["file"]["name"] . "<br />";
                            echo "Type: " . $_FILES["file"]["type"] . "<br />";
                            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
                            echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
                
                            if (file_exists("Proposals/" . $_FILES["file"]["name"]))
                            {
                                echo $_FILES["file"]["name"] . " already exists. ";
                            }
                            else
                            {
                                move_uploaded_file($_FILES["file"]["tmp_name"],
                                "Proposals/" . $_FILES["file"]["name"]);
                                echo "Stored in: " . "Proposals/" . $_FILES["file"]["name"];
                            }
                        }
                    } else {
                        echo "Invalid file";
                    }
                

                每次我尝试上传文件时,无论是 doc 还是 png,它都会输出:

                Everytime I try to upload a file, whether it's a doc or a png it outputs this:

                Upload: 
                Type: 
                Size: 0 Kb
                Temp file: 
                already exists.
                

                最终没有任何东西上传到提案文件夹.所以我有两个问题:
                1) 我的代码有什么问题?
                2) 它重定向到upload_file.php 并显示一条消息.有没有办法真正返回主页并显示成功的文本?

                And nothing ends up getting uploaded to the Proposals folder. So I have 2 questions:
                1) what is the problem with my code?
                2) it redirects to upload_file.php and displays a message. Is there a way to actually go back the main page and display text that it was successful?

                推荐答案

                第一个问题已经回答,不再回答.

                The first question has already been answered so I won't answer it again.

                2) 它重定向到upload_file.php 并显示一条消息.有没有办法真正返回主页并显示成功的文本?

                2) it redirects to upload_file.php and displays a message. Is there a way to actually go back the main page and display text that it was successful?

                通常最好在重定向页面上显示成功消息,但您可以通过以下两种方式解决:

                It is normally better to show the success message on the redirected page but you can solve this two ways:

                • 存储上一个 URL(或知道它)并使用 header("Location: old_page.php"); 重定向到它;
                • 将表单的目标设为 iframe.这意味着主页本身不会重定向,您只需从upload_file.php(将加载到iframe)中冒泡响应,从而为您的应用无缝上传.

                这也不是获得文件扩展名的最佳方式:

                Also this is not the best way to get a file extension:

                $extension = end(explode(".", $_FILES["file"]["name"]));
                

                尝试改用 pathinfo:http://php.net/manual/en/function.pathinfo.php

                Try using pathinfo instead: http://php.net/manual/en/function.pathinfo.php

                $extension = pathinfo( $_FILES["file"]["name"], PATHINFO_EXTENSION);
                

                还有你的 if 语句:

                if ($extension!=".doc" || $extension!=".doc"
                    && ($_FILES["file"]["size"] < 200000)
                    && in_array($extension, $allowedExts)) {
                

                尽管这是 in_array 的一小部分用法,但我仍然建议将其取出并使用更实用的方法来搜索数组:http://php.net/manual/en/function.array-search.php

                Even though this is a tiny usage of in_array I still would recommend taking that out and using a more practical method of searching arrays: http://php.net/manual/en/function.array-search.php

                所以你的 if 会变成这样:

                So your if would become something like:

                if (($_FILES["file"]["size"] < 200000) && array_search($extension, $allowedExts)!==false) {
                

                这篇关于HTML/PHP 文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                  • <bdo id='zxZsY'></bdo><ul id='zxZsY'></ul>

                    <small id='zxZsY'></small><noframes id='zxZsY'>

                        <legend id='zxZsY'><style id='zxZsY'><dir id='zxZsY'><q id='zxZsY'></q></dir></style></legend>
                          <tbody id='zxZsY'></tbody>

                        1. <i id='zxZsY'><tr id='zxZsY'><dt id='zxZsY'><q id='zxZsY'><span id='zxZsY'><b id='zxZsY'><form id='zxZsY'><ins id='zxZsY'></ins><ul id='zxZsY'></ul><sub id='zxZsY'></sub></form><legend id='zxZsY'></legend><bdo id='zxZsY'><pre id='zxZsY'><center id='zxZsY'></center></pre></bdo></b><th id='zxZsY'></th></span></q></dt></tr></i><div id='zxZsY'><tfoot id='zxZsY'></tfoot><dl id='zxZsY'><fieldset id='zxZsY'></fieldset></dl></div>
                          <tfoot id='zxZsY'></tfoot>