首页 文章

将上传集成到现有表单(PHP)

提问于
浏览
1

我的工作php页面上有一个现有的表单 .

<form action="index.php" method="POST">

...

我想添加图片上传,但脚本需要enctype?

<form enctype="multipart/form-data" action="index.php" method="POST">

...

是否可以将enctype =“multipart / form-data”添加到提交其他非图像输入数据的表单中,或者我需要在页面上添加2个表单?

4 回答

  • 1

    添加属性enctype = "multipart/form-data"不会阻止表单接受来自其他输入类型(如文本,复选框或无线电)的数据 . 您可以使用单个表单提交所有数据 .

  • 1

    你可以将属性添加到 form 和里面的字段,这将工作!

  • 1

    具有文件上载的表单也可以使用 enctype="multipart/form-data" 发布其他输入字段 .

  • 3

    enctype 属性通过MIME类型指定表单数据在请求中的编码方式 . 我知道的两个有效的enctype是 application/x-www-form-urlencodedmultipart/form-data .

    在引擎盖下,当你指定没有类型时,你会得到 application/x-www-form-urlencoded . 这表明在发送请求时,您的浏览器将对表单数据进行编码,就像将其作为GET参数发送一样:请求正文看起来像 foo=bar&bar=baz&frob=this%20has%20four%20words . 虽然完全适用于文本字段,但这种表示对于文件传输来说非常尴尬,特别是对于二进制文件而言,其大小可能会增加三倍 . 为了避免发生这种情况,不可能使用 application/x-www-form-urlencoded 来发送文件 .

    另一方面, multipart/form-data 导致了非常不同的表现形式 . 我不需要't remember the exact details, but each input field in your form can have a distinct MIME type and encoding. This means your browser can easily send binary files to a site, along with form data. However, the general structure of the request creates a relatively large overhead, so you should use it only when it'(即你想发送文件时) .

    所以,是的,设置 enctype="mutlipart/form-data" 仍然会让你有常规字段 . 它们只是代表另一种方式,但这对你来说是完全透明的 .

相关问题