HTML/PHP 文件上传

2022-01-09 00:00:00 file file-upload upload php html

所以我希望用户只能上传文档或 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) {

相关文章