Question

I have a form and the action of the form is submit.php, what I am trying to do on submit.php to upload a file image, but when I click submit and print_r $_FILE an empty array appears. What am I doing wrong? I dont want to upload the file on the same page as the form. Here is the form:

<form action="in-the-press-submit.php" method="post">

<p>
<label for="title">Title:</label>
<input type="input" name="title" id="title">
</p>

<p>
<label for="posted-on">Posted On:</label>
<input type="date" name="posted-on" id="posted-on">
</p>

<p>
<label for="description" style="vertical-align:top;">Description:</label>
<textarea rows="20" cols="70" name="description" id="description"></textarea>
</p>

<p>
<label for="image">Image:</label>
<input type="file" name="image" id="image">
</p>

<p>
<input type="submit" name="submit" id="submit">
</p>

</form>

and here is my code in the submit page:

<?php

  if ($_FILES["file"]["error"] > 0) {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
  } else {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "/image-test/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "/image-test/" . $_FILES["file"]["name"];
  }

?>
Was it helpful?

Solution

Try this

<form action="in-the-press-submit.php" method="post" enctype="multipart/form-data">

Documentation : http://www.w3schools.com/php/php_file_upload.asp

OTHER TIPS

You need enctype="multipart/form-data" in your form tag:

<form action="in-the-press-submit.php" method="post" enctype="multipart/form-data">

See the php manual on POST method uploads.

the reason that you need to include enctype=multipart/form-data in the form tag is :

application/x-www-form-urlencoded is the standard and default way to POST a form without attached files.

multipart/form-data is the standard way to POST a form with attached file(s) because this encoding allows entire files to be included in the data

In order to upload a file add

enctype = "multipart/form-data"

in form tag.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top