简体   繁体   English

$ _POST不起作用,$ _ GET起作用

[英]$_POST doesn't work, $_GET does

I've got kind of an annoying problem. 我有一个烦人的问题。 When I submit the following code using method="post" and looking for the $_POST variable, the code doesn't recognize it. 当我使用method =“ post”提交以下代码并寻找$ _POST变量时,代码无法识别它。 When I switch to method="get" and look for the $_GET variable it works fine. 当我切换到method =“ get”并寻找$ _GET变量时,它可以正常工作。 I would prefer not to use Get. 我不想使用Get。 Any suggestions? 有什么建议么? My final code will contain other inputs as well, however, I want the file input to be optional. 我的最终代码也将包含其他输入,但是,我希望文件输入是可选的。 So I need to process the code only if the file input is actually sent. 因此,仅当文件输入实际发送时,我才需要处理代码。 Thanks for your help! 谢谢你的帮助!

 //form.html.php
<form action="index.php" method="post" enctype="multipart/form-data">
<label id="upload">Select file to upload:
<input type="file" id="upload" name="foobar" /></label>
<input type="submit" value="Submit" />
</form>

//index.php
if(isset($_POST['foobar']))
{
//some code
}

However, this works fine 但是,这很好

//form.html.php
<form action="index.php" method="get" enctype="multipart/form-data">
<label id="upload">Select file to upload:
<input type="file" id="upload" name="foobar" /></label>
<input type="submit" value="Submit" />
</form>

//index.php
if(isset($_GET['foobar']))
{
//some code
}

要获取文件,请使用$_FILES ,并且必须使用post方法。

You are uploading a file and do not have any inputs other then the "file" type input. 您正在上载文件,除了“文件”类型输入外,没有其他输入。 Therefore there will not be any $_POST values sent put $_FILES array. 因此,不会将任何$ _POST值发送到放置$ _FILES数组。 And your form needs to have method="post" set. 并且您的表单需要设置method =“ post”。

Check this code out 签出此代码

echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM