17

I need help with my form. There's a mix of input, textarea and file upload that i want to enter into the database..

What do I use in the ? Do I use the normal form attribute :

  <form action="" method="">

or

<form enctype="" action="" method="">

Please have it in mind that, I have to do this in a single page, and the picture upload must be done along with other text input.

Thanks for your time.

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 1
    Good reading: http://www.php.net/manual/en/features.file-upload.post-method.php – j08691 Jun 14 '12 at 20:05
  • You should do some basic learnings first before using a language feature. E.g. do you already know how to do that with HTML and PHP? If not, why not take a read of the according section in the PHP manual? --- possible duplicate of [Why not always use enctype="multipart/form-data"?](http://stackoverflow.com/questions/1039166/why-not-always-use-enctype-multipart-form-data) – hakre Jun 15 '12 at 10:47
  • @hakre ... I've done several forms, but this was an exception. –  Jun 15 '12 at 10:50
  • Especially then, it's worth to re-read the chapters in the PHP manual because it contains information about less common problems, too. See the link by @j08691. – hakre Jun 15 '12 at 10:51

5 Answers5

20

You must use enctype="multipart/form-data" for file uploading, this will also work fine for non-file upload forms.

Matthew
  • 24,703
  • 9
  • 76
  • 110
7

You need to set enctype="multipart/form-data" and use method="post" for any form that includes a file input. This won't stop you from including other types of fields.

(The way those fields will be submitted to the server will change, but your form parsing library will deal with the differences automatically, you only need to worry about them if you are parsing the raw input yourself).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
5

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

submit.php being, in this case, the external PHP script that will process your form ( if you decide to use PHP ). But you can name the .php script whatever you like ( e.g. cats.php ).

The uploaded file/image data will be stored inside $_FILES, and all the textfield, textarea, radio buttons, check boxes and other data will reside inside the $_POST superglobal.

When submit.php receives the submitted form you can do all kinds of processing on it such as validating that user has submitted the correct type of file/image, store the file path of the file/image in your local database( client/server or file system based ), and much more.

Make sure to validate user input client side and server side too.

Anthony Rutledge
  • 6,980
  • 2
  • 39
  • 44
Bob
  • 1,355
  • 5
  • 19
  • 38
3
<form enctype="multipart/form-data" action="yourpage.php" method="post">

You'll need the enctype attribute if you want the file upload to work. FYI, a form can contain every field type, including file uploads, and work just fine.

John Conde
  • 217,595
  • 99
  • 455
  • 496
0

In Classic ASP I had to access my textfield as load.getFileData("textfield") instead of the standard Request("textfield") when using the enctype="multipart/form-data"