简体   繁体   English

是否可以上传文件并以单一形式提交文字?

[英]Is it possible with upload a file AND submit text in a single form?

I'm writing a plugin for wordpress where certain information like a name and e-mailaddress and some info is stored in an SQL database table. 我正在为wordpress编写一个插件,其中某些信息(如名称和电子邮件地址)和一些信息存储在SQL数据库表中。 I've got that working perfectly. 我有完美的工作。 But this information also needs a picture to go with it. 但是这些信息也需要一张图片。

So when wordpress admin fills in the form, has the file selected and clicks submit I want the file to be uploaded into a directory and its location stored in a database along with the name and e-mailaddress. 因此,当wordpress管理员填写表单时,选择文件并单击提交我希望将文件上载到目录中,并将其位置与名称和电子邮件地址一起存储在数据库中。

All this preferably needs to be done in just one form. 所有这些优选地需要以一种形式完成。

Is this possible? 这可能吗? Or is there another way that would make the end user click only once on a sumbit button. 或者是否有另一种方法可以让最终用户在sumbit按钮上只点击一次。

Yes, it's possible. 是的,这是可能的。 Just make sure you set the form enctype="multipart/form-data" and that you use type="file" for file input and regular inputs ( type="text" , etc) for texts. 只需确保设置形式enctype="multipart/form-data"并使用type="file"进行文件输入,并使用常规输入( type="text"等)进行文本。

You'll need to write a code to move uploaded files to a location you want, as well as to add its info to the database. 您需要编写代码以将上载的文件移动到所需的位置,并将其信息添加到数据库中。

See these two nice tutorials: here and here . 看到这两个很好的教程: 这里这里

Yes, it should be possible. 是的,它应该是可能的。

Just ensure that your <form> has the attributes enctype="multipart/form-data" and method="post" 只需确保您的<form>具有enctype="multipart/form-data"method="post"属性

By PHP side after submit, you can use $_POST variables as usual, plus $_FILES for handling uploads with <input type="file"> in the same form. 通过提交后的PHP方面,您可以像往常一样使用$_POST变量,加上$_FILES以相同的形式处理<input type="file">上传。

See this PHP manual page for details on file upload. 有关文件上载的详细信息,请参阅此PHP手册页

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM