php 如何解决在同一页面上包含文件上传和其他文本输入的 <form>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11040535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve <form> that includes file upload and other text input, on the same page
提问by Matthew
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.
谢谢你的时间。
回答by Matthew
You must use enctype="multipart/form-data"for file uploading, this will also work fine for non-file upload forms.
您必须enctype="multipart/form-data"用于文件上传,这也适用于非文件上传表单。
回答by Quentin
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.
您需要为包含文件输入的任何表单设置enctype="multipart/form-data"和使用method="post"。这不会阻止您包含其他类型的字段。
(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).
(这些字段提交到服务器的方式会改变,但你的表单解析库会自动处理这些差异,如果你自己解析原始输入,你只需要担心它们)。
回答by Bob
<form enctype="multipart/form-data" method="post" action="submit.php">
<form enctype="multipart/form-data" method="post" action="submit.php">
submit.phpbeing, in this case, the external PHP script that will process your form ( if you decide to use PHP ). But you can name the .phpscript whatever you like ( e.g.cats.php).
submit.php在这种情况下,是将处理您的表单的外部 PHP 脚本(如果您决定使用 PHP)。但是您可以随意命名.php脚本(例如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 $_POSTsuperglobal.
上传的文件/图像数据将存储在 中$_FILES,所有文本字段、文本区域、单选按钮、复选框和其他数据都将位于$_POST超全局中。
When submit.phpreceives 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.
当submit.php收到提交的表单,你可以做各种关于它的处理,如验证用户提交了正确类型的文件/图像,存储在本地数据库(客户端/服务器或文件系统中的文件/图像文件的路径基于), 以及更多。
Make sure to validate user input client side and server side too.
确保验证用户输入客户端和服务器端。
回答by John Conde
<form enctype="multipart/form-data" action="yourpage.php" method="post">
You'll need the enctypeattribute if you want the file upload to work. FYI, a form can contain every field type, including file uploads, and work just fine.
enctype如果您希望文件上传工作,您将需要该属性。仅供参考,表单可以包含所有字段类型,包括文件上传,并且工作正常。
回答by user5981305
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"
在经典 ASPload.getFileData("textfield")中,Request("textfield")当使用 enctype="multipart/form-data" 时,我必须访问我的文本字段而不是标准

