php 如何:Drupal 文件上传表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1253094/
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
Howto: Drupal File Upload Form
提问by Nick Heiner
I'm having difficulty figuring out how to write a module with a form that uploads files, in Drupal 6. Can anyone explain this, or point me to a good example/documentation discussing it?
我很难弄清楚如何在 Drupal 6 中编写一个带有上传文件表单的模块。谁能解释一下,或者给我一个很好的例子/文档来讨论它?
EDIT:
编辑:
Here is entirely what I am trying to do:
这完全是我想要做的:
- User uploads a .csv
- Module reads the first line of the file to get fields
- User matches csv fields with db fields
- Each csv line is saved as a node (preview it first)
- 用户上传 .csv
- 模块读取文件的第一行以获取字段
- 用户将 csv 字段与 db 字段匹配
- 每个csv行保存为一个节点(先预览)
So far, I can do 1, 2, and 4 successfully. But it's unclear exactly how the steps should interact with each other ($form_state['redirect']? how should that be used?), and what the best practices are. And for 3, should I save that as session data?
到目前为止,我可以成功地做1、2和4。但目前还不清楚这些步骤应该如何相互交互($form_state['redirect']?应该如何使用?),以及最佳实践是什么。对于 3,我应该将其保存为会话数据吗?
How do I pass the file data between the various steps?
如何在各个步骤之间传递文件数据?
I know that node_import exists, but it's never worked for me, and my bug requests go ignored.
我知道 node_import 存在,但它从来没有为我工作过,我的错误请求被忽略了。
2nd EDIT:I used this at the start and end of every page that needed to deal with the file:
第二次编辑:我在需要处理文件的每个页面的开头和结尾都使用了它:
$file = unserialize($_SESSION['file']);
//alter $file object
$_SESSION['file'] = serialize(file);
I'm not sure it it's best practices, but it's been working.
我不确定这是最佳实践,但它一直在工作。
回答by googletorp
This is not too difficult, you can see some info here. An example of a form with only a file upload.
这不是太难,你可以在这里看到一些信息。仅上传文件的表单示例。
function myform_form($form_state) {
$form = array('#attributes' => array('enctype' => 'multipart/form-data'));
$form['file'] = array(
'#type' => 'file',
'#title' => t('Upload video'),
'#size' => 48,
'#description' => t('Pick a video file to upload.'),
);
return $form;
}
EDIT:
编辑:
Now to save the file use the file_save_uploadfunction:
现在使用file_save_upload函数保存文件:
function myform_form_submit($form, $form_state) {
$validators = array();
$file = file_save_upload('file', $validators, 'path');
file_set_status($file, FILE_STATUS_PERMANENT);
}
2nd EDIT:
第二次编辑:
There's a lot of questions and ways to do the things you described. I wont go to much into the actual code of how to handle a csv file. What I would suggest is that you use the file id to keep track of the file. That would enable you to make urls that take a fid and use that to load the file you want to work on. To get from your form to the next step, you can use the #redirectform property to get your users to the next step. From there is really depends how you do things, what you'll need to do.
有很多问题和方法可以做你描述的事情。我不会过多介绍如何处理 csv 文件的实际代码。我建议您使用文件 ID 来跟踪文件。这将使您能够制作带有 fid 的 url 并使用它来加载您想要处理的文件。要从您的表单进入下一步,您可以使用#redirect表单属性让您的用户进入下一步。从那里真的取决于你如何做事,你需要做什么。

