Search code examples
phphtmlpostfile-uploadenctype

Setting enctype attribute for mixed data types, including file


I am new to PHP and HTML, and have trouble understanding the enctype attribute of the form when using POST. In all examples of File Upload using PHP, it is written that I should set enctype as multipart/form-data (which, as far as I understand, is used for files). However in all these examples, the only input on the form is file input.

My question is, what if I want to have form with mixed inputs? Particularly, both text and file. Then, if I set enctype to multipart/form-data, will I still be able to access both files and text from PHP using $_FILES and $_POST?

Thank you


Solution

  • Use multipart/form-data.

    It converts each piece of data to a part in a multi-part MIME request (and then does some specific encoding of each part). It isn't just for files, and if you are using PHP then $_POST will be populated normally.

    The alternative (application/x-www-form-urlencoded) basically builds a query string. For a POST request it makes that query string the request body. This format doesn't have any provision for encoding files.