0

我之前有几个上传表单可以工作,但是,即使在几乎复制了我以前的代码之后,这似乎也不起作用,我更喜欢在一个 php 脚本文件中完成所有工作,因此它全部在这个文件中生成。

我的表格:

<form action="" method="post" enctype="multipart/form-data">
    <ul>
        <li>
            <label for="file">File : </label>
            <input type="file" id="file" name="file" required="required" />
        </li>
        <li>
            <input type="submit" value="Upload" />
        </li>
    </ul>
</form>

我的php上传:

if(!empty($_POST['file']))
{
    echo "Found.";
    $exts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $ext = end($temp);
    if((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 20000)
    && in_array($ext, $exts))
    {
        if($_FILES["file"]["error"] > 0)
        {
            $result = "Error Code: " . $_FILES["file"]["error"] . "<br />";
        }
        else
        {
            $scandir = scandir("/images/news/");
            $newname = (count($scandir-2)) . $ext;
            move_uploaded_file($_FILES["file"]["tmp_name"],"/images/news/" . $newname);
            $ulink = "/images/news/" . $newname;
            $result = "Success, please copy your link below";
        }
    }
    else
    {
        $result = "Error.";
    }
}

当我上传 .png 图像时,页面似乎只是刷新了,我已将其放在echo "Found.";那里以检查它是否有任何内容,$_POST["file"]但似乎没有任何内容。

我不明白为什么页面没有正确提交。我已更改action=""action="upload.php"确保它指向同一页面但仍然没有。

4

4 回答 4

6

使用$_FILES['file']而不是$_POST['file'].

在http://www.php.net/manual/en/features.file-upload.post-method.php阅读有关 $_FILES 的更多信息

于 2013-09-19T12:03:13.133 回答
1

替换$_POST['file']$_FILES['file']并设置action=""

于 2013-09-19T12:04:43.780 回答
0

试试这个.. 因为 $_POST 不适用于文件,对于文件我们使用 $_FILES..

if(!empty($_FILES['file']))
{
    echo "Found.";
    $exts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $ext = end($temp);
    if((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 20000)
    && in_array($ext, $exts))
    {
        if($_FILES["file"]["error"] > 0)
        {
            $result = "Error Code: " . $_FILES["file"]["error"] . "<br />";
        }
        else
        {
            $scandir = scandir("/images/news/");
            $newname = (count($scandir-2)) . $ext;
            move_uploaded_file($_FILES["file"]["tmp_name"],"/images/news/" . $newname);
            $ulink = "/images/news/" . $newname;
            $result = "Success, please copy your link below";
        }
    }
    else
    {
        $result = "Error.";
    }
}
于 2013-09-19T12:09:44.983 回答
0

我不会只检查 $_FILES 变量。我会命名提交输入并检查提交输入是否已提交。这样,您可以检查是否在未选择文件的情况下按下按钮并提示用户。

像这样:

<form action="" method="post" enctype="multipart/form-data">
    <ul>
        <li>
            <label for="file">File : </label>
            <input type="file" id="file" name="file" required="required" />
        </li>
        <li>
            <input type="submit" value="Upload" name="upload"/>
        </li>
    </ul>
</form>

然后您可以检查该值的 post 变量。

像这样:

if(!empty($_POST['upload']))
{
    echo "Found.";
    $exts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $ext = end($temp);
    if((($_FILES["file"]["type"] == "image/gif")
        || ($_FILES["file"]["type"] == "image/jpeg")
        || ($_FILES["file"]["type"] == "image/jpg")
        || ($_FILES["file"]["type"] == "image/pjpeg")
        || ($_FILES["file"]["type"] == "image/x-png")
        || ($_FILES["file"]["type"] == "image/png"))
        && ($_FILES["file"]["size"] < 20000)
        && in_array($ext, $exts))
    {
        if($_FILES["file"]["error"] > 0)
        {
            $result = "Error Code: " . $_FILES["file"]["error"] . "<br />";
        }
        else
        {
            $scandir = scandir("/images/news/");
            $newname = (count($scandir-2)) . $ext;
            move_uploaded_file($_FILES["file"]["tmp_name"],"/images/news/" . $newname);
            $ulink = "/images/news/" . $newname;
            $result = "Success, please copy your link below";
        }
    }
    else
    {
        $result = "Error.";
    }
}
于 2013-09-19T12:15:03.487 回答