PHP文件上传什么都不做

PHP文件上传什么都不做,php,forms,file,upload,Php,Forms,File,Upload,我以前有过几个上传表单,但是,即使在几乎复制了我以前的代码之后,这个似乎也不起作用,我更喜欢在一个php脚本文件中完成这一切,所以它都是在这个文件中生成的 我的表格: <form action="" method="post" enctype="multipart/form-data"> <ul> <li> <label for="file">File : </label>

我以前有过几个上传表单,但是,即使在几乎复制了我以前的代码之后,这个似乎也不起作用,我更喜欢在一个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图像时,页面似乎只是在刷新,我已经放置了找到的回音。;在那里检查它在$\u POST[file]中是否有任何内容,但它似乎没有任何内容

我不明白为什么页面提交不正确。我已将action=更改为action=upload.php,以确保它指向相同的页面,但仍然没有任何内容。

使用$\u FILES['file']而不是$\u POST['file']


阅读有关$\u文件的更多信息,请访问

将$\u POST['file']替换为$\u FILES['file'],并设置action=。

尝试以下操作。。。。因为$\u POST不能处理文件,所以对于文件,我们使用$\u文件

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.";
    }
}

我不会只检查$\u 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>
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.";
    }
}
然后可以检查post变量的值

像这样:

<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>
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.";
    }
}

我真不敢相信它这么简单!谢谢你,我应该知道这件事的。这件事发生在我们当中最好的人身上。只要将这个问题标记为现在已回答。我会在9分钟内回答,当网站允许我回答时。