Reputation: 738
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. When I switch to method="get" and look for the $_GET variable it works fine. I would prefer not to use 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
}
Upvotes: 0
Views: 379
Reputation: 551
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. And your form needs to have method="post" set.
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"];
Upvotes: 1