你好,
我们如何在Drupal 7自定义表单中添加文件上传字段。上传并提交后,文件将保存在/sites/all/default/file中。
谢谢
欢迎各位兄弟 发布技术文章
这里的技术是共享的
下面 有大用
function form_example_tutorial_10
Same name and namespace in other branches
Example 10: A form with a file upload field.
This example allows the user to upload a file to Drupal which is stored physically and with a reference in the database.
See also
form_example_tutorial_10_submit()
form_example_tutorial_10_validate()
Related topics
Example: Form API
Examples demonstrating the Drupal Form API.
1 string reference to 'form_example_tutorial_10'
File
form_example/
form_example_tutorial.inc, line 740
This is the Form API Tutorial from the handbook.
Code
function form_example_tutorial_10($form_state) {
// If you are familiar with how browsers handle files, you know that
// enctype="multipart/form-data" is required. Drupal takes care of that, so
// you don't need to include it yourself.
$form['file'] = array(
'#type' => 'file',
'#title' => t('Image'),
'#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function form_example_tutorial_10_validate($form, &$form_state) {
$file = file_save_upload('file', array(
// Validates file is really an image.
'file_validate_is_image' => array(),
// Validate extensions.
'file_validate_extensions' => array(
'png gif jpg jpeg',
),
));
// If the file passed validation:
if ($file) {
// Move the file into the Drupal file system.
if ($file = file_move($file, 'public://')) { # 'public://' 后面可以跟文件 ,比如 'public://myfiledir/'.$file->filename
// Save the file for use in the submit handler.
$form_state['storage']['file'] = $file;
}
else {
form_set_error('file', t("Failed to write the uploaded file to the site's file folder."));
}
}
else {
form_set_error('file', t('No file was uploaded.'));
}
}
function form_example_tutorial_10_submit($form, &$form_state) {
$file = $form_state['storage']['file'];
// We are done with the file, remove it from storage.
unset($form_state['storage']['file']);
// Make the storage of the file permanent.
$file->status = FILE_STATUS_PERMANENT;
// Save file status.
file_save($file);
// Set a response to the user.
drupal_set_message(t('The form has been submitted and the image has been saved, filename: @filename.', array(
'@filename' => $file->filename,
)));
}
Drupal 7 forms API: File upload
function portal_upload_form($form, $form_state) {
$form['file'] = array(
'#type' => 'file',
'#title' => t('Choose a file'),
);
$form['document_submit_button'] = array(
'#type' => 'submit',
'#value' => t('upload'),
);
return $form;
}
function portal_upload_form_submit($form, &$form_state) {
$file = $form_state['values']['file'];
$validators = array();
$file = file_save_upload('file', $validators, 'public://');
来自 https://stackoverflow.com/questions/11017089/drupal-7-forms-api-file-upload/15461585
你好,
我们如何在Drupal 7自定义表单中添加文件上传字段。上传并提交后,文件将保存在/sites/all/default/file中。
谢谢
评论
我可以推荐看
我可以推荐查看示例模块吗?如果您查看有关表单的最后一个教程,它显示了如何上传文件、验证和保存。
form_example_tutorial_10
登录或注册以发表评论
嗨,我尝试了代码
嗨,我尝试了 form_example_tutorial_10 中的代码
该文件已上传到默认内的“文件”文件夹中。但出现此错误:
注意:未定义的属性:file_save() 中的 stdClass::$uri(includes\file.inc 的第 575 行)。
PDOException: SQLSTATE[23000]: 完整性约束违规:1062 重复条目 '' 键 'uri': INSERT INTO {file_managed} (filesize, status, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); drupal_write_record() 中的数组( [:db_insert_placeholder_0] => 0 [:db_insert_placeholder_1] => 1 [:db_insert_placeholder_2] => 1334752869 )(includes\common.inc 的第 6776 行)。
你能指点一下吗?
登录或注册以发表评论
你能展示你的代码吗?
你能展示你的代码吗?
从错误消息看来,您似乎是将 uri 放入 file_save() 中,但您应该改用文件对象。
登录或注册以发表评论
这是:在 hook_menu 下:
这里是:
在 hook_menu 下:
$form['file'] = array(
'#type' => 'file',
'#title' => t('Image'),
'#description' => t('上传文件,允许扩展名:jpg , jpeg, png, gif'),
);
在 hook_validation 下:
$file = file_save_upload('file', array(
'file_validate_is_image' => array(),
'file_validate_extensions' => array('png gif jpg jpeg'),
));
if ($file) {
if ($file = file_move($file, 'public://')) {
$form_state['values']['file'] = $file;
}
else {
form_set_error('file', t('无法将上传的文件写入站点的文件夹。'));
}
}
else {
form_set_error('file', t('没有上传文件。'));
}
}
在 hook_submit 下:
$file=$form_state['values']['file'];
取消设置($form_state['values']['file']);
$file->status = FILE_STATUS_PERMANENT;
文件保存($文件);
drupal_set_message(t('表单已提交,图片已保存,文件名:@filename.', array('@filename' => $file->filename)));
登录或注册以发表评论
适用于
对我来说很好用。
登录或注册以发表评论
谢谢它起作用了!
谢谢它起作用了!
登录或注册以发表评论
多个文件到多个节点