PHP 上传 - 为什么 isset($_POST['submit']) 总是 FALSE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3337682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:23:20  来源:igfitidea点击:

PHP upload - Why isset($_POST['submit']) is always FALSE

php

提问by a2011

I have the following code sample upload3.php:

我有以下代码示例upload3.php:

<html>
<head>
<title>PHP Form Upload</title>
</head>
<body>

<form method='post' action='upload3.php' enctype='multipart/form-data'>
    Select a File:
    <input type='file' name='filename' size='10' />
    <input type='submit' value='Upload' />
</form>

<?php

if (isset($_POST['submit']))
{
    echo "isset submit";
}
else 
{
    echo "NOT isset submit";
}

?>

</body>
</html>

The code always returns "NOT isset submit". Why does this happen? Because the same script upload3.php calls itself?

代码总是返回“NOTisset submit”。为什么会发生这种情况?因为同一个脚本upload3.php调用了自己?

回答by ashurexm

You do not have your submit button named:
Change

您没有将提交按钮命名为:
更改

<input type='submit' value='Upload' />

To:

到:

<input type='submit' value='Upload' name="submit"/>

回答by Michael Mac McCann

Two things:

两件事情:

You'll want to try array_key_exists instead of isset when using arrays. PHP can have some hinky behavior when using isset on an array element.

使用数组时,您需要尝试使用 array_key_exists 而不是 isset。在数组元素上使用 isset 时,PHP 可能会有一些奇怪的行为。

http://www.php.net/manual/en/function.array-key-exists.php

http://www.php.net/manual/en/function.array-key-exists.php

if (array_key_exists('submit', $_POST)) { }

if (array_key_exists('submit', $_POST)) { }

Second, you need a name attribute on your button ( "name='submit'" )

其次,您的按钮上需要一个 name 属性( "name='submit'" )

回答by Artefacto

Because you don't have any form element whose nameproperty is submit.

因为您没有任何name属性为submit.

Try to use var_dump($_POST)to see the keys that are defined.

尝试使用var_dump($_POST)查看定义的键。

Notice that files are an exception; they're not included in $_POST; they're stored in the filesystem and they're metadata (location, name, etc) is in the $_FILESsuperglobal.

请注意,文件是一个例外;他们不包括在$_POST;它们存储在文件系统中,并且它们的元数据(位置、名称等)位于$_FILES超全局中。

回答by CharlesLeaf

Try looking at the REQUEST_METHODand see if it's POST. It's a little bit nicer.

尝试查看REQUEST_METHOD并查看它是否是 POST。它更好一点。

回答by Kingk

<input type='submit' value='Upload' />

should be

应该

<input type='submit' value='Upload' name='subname'/>

and that subname should be in $_POST[' ']

并且该子名应该在 $_POST[' '] 中

it will look like

它看起来像

if (isset($_POST['subname']))
{
    echo "isset submit";
}
else 
{
    echo "NOT isset submit";
}