0

I have a file upload input attribute inside a regular form to upload a photo.

<form class="reg-page" action="phpMailer/sendEmail.php" method="post" enctype="multipart/form-data">

  <div class="form-group">
    <label>Name <span class="color-red">*</span>
    </label>
    <input type="text" class="form-control margin-bottom-20" name="name" required>
  </div>
  <div class="form-group">
    <label>Email <span class="color-red">*</span>
    </label>
    <input type="text" class="form-control" name="email" required>
  </div>
  <label class="control-label">Photo <span class="color-red">*</span>
  </label>
  <input type="file" class="file" name="photo">
  <input type="submit" class="btn btn-apply" value="submit"></input>
</form>

I used PHP to process the POST parameters and the $_FILES parameters, but it seems that $_FILES array is empty when I try to var_dump and see if files are received by the backend.

var_dump($_FILES);

Any reason why this is happening?

Edit:

Here's my PHP file,

$msg = "";
foreach ($_POST as $key => $value) {
    if ($key != "photo") {
        $msg = $msg . "<br/>" . $key . " : " . $value;
    }
}
echo $msg."<br/>";

var_dump($_FILES);

if(!empty($_FILES))
{
    echo "Files Not Empty";
}

This prints all the $_POST parameters fine

and then

array(0) { }

for $_FILES

Seyong Cho
  • 1,067
  • 1
  • 10
  • 29

2 Answers2

0

Try the following lines to see it.

echo "<pre>";
var_dump($_FILES['photo']); //or print_r
echo "</pre>";
kimbarcelona
  • 1,136
  • 2
  • 8
  • 19
  • Sorry I didn't put it in the question, but the submit button is not an issue. I modified the form in the question to have the submit button. Basically I have confirmed all $_POST parameters, just $_FILES in the php file is empty. – Seyong Cho Jan 06 '16 at 02:27
-1

As suggested on the comments the <form> (Form Tag) tag does not have the attribute class that was indeed causing the problem.

So the code to solve the problem would be:

<form action="phpMailer/sendEmail.php" method="post" enctype="multipart/form-data">
Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
  • 1
    In deed, `class` is a [Global Attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes), that can be present on any element. By the way, W3Schools is not a good source. – Henrique Barcelos Jan 06 '16 at 13:14
  • `
    ` tag DO HAVE `class` attribute, as it is a global attribute. The problem is something else.
    – Henrique Barcelos Jan 06 '16 at 17:20
  • I saw that reference from the MDN but there is nowhere to mention the association between the class attribute in a form and the possible cause beeing the association with the enctype attribute. I've tested here on my end and the same happened. I did the test using JSF+primefaces, pure JSP+servlet, old ASP+vb and also PHP. All of then had the same problem, the file wont get uploaded. Again my guess is that it is a problem with the attribute class and enctype. And where did you get this information that W3Schools is not a good source? – Jorge Campos Jan 06 '16 at 17:31
  • There might be a problem related to JSF, JSP or ASP, but OP tagged his question as [tag:php]. I've been using PHP for years and always had `enctype` and `class` attribute in my forms with no issues at all. – Henrique Barcelos Jan 06 '16 at 17:42
  • About W3Schools there is this [thread](http://meta.stackoverflow.com/questions/280478/why-not-w3schools-com). Their name is misleading, they have nothing to do with W3C, there are lots of bad examples, that lead the creation of [W3Fools](http://www.w3fools.com/), which is now disabled. – Henrique Barcelos Jan 06 '16 at 17:45
  • Point taken about the w3school, thanks for that. But I would not tend to believe that there is a problem with every language and also PHP (As I tested it here). Maybe it will end up being some sort of configuration on the server side (also in PHP, since the problem happened here with it). But let it be. It solve the OP's problem. Unfortunately I couldn't find any reference about this behavior. I will change the reference though. – Jorge Campos Jan 06 '16 at 17:52
  • 1
    There is no sense in that. `class` attribute does not reach the server side ever. – Henrique Barcelos Jan 06 '16 at 17:56