PHP文件上传没有任何作用

时间:2013-09-19 12:01:51

标签: php forms file upload

我之前有过几个上传表单,但是,即使在几乎复制我之前的代码之后,这似乎也不起作用,我更喜欢在一个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 个答案:

答案 0 :(得分:6)

使用$_FILES['file']代替$_POST['file']

http://www.php.net/manual/en/features.file-upload.post-method.php

了解有关$ _FILES的更多信息

答案 1 :(得分:1)

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

答案 2 :(得分: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.";
    }
}

答案 3 :(得分: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.";
    }
}
相关问题