欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Drupal 6 file upload module自定义文件上传 有大用

shiping1 的头像
I see you have many changes made since the first page. Copying and changing it to a final answer... This code does not contain any critical fixes. What you have in the question now should be working. I just compared yours with a module I had for Drupal 6. However it requires some changes to best practices. See inline comments.
正确答案
<?php
function upload_example_menu() {
  $items = array();
  $items['upload_example'] = array(
    'title' => 'Upload', // You don't use t() for menu router titles. See 'title callback' that defaults to t().
    'page callback' => 'drupal_get_form',
    'page arguments' => array('upload_example_form'),
    'description' => t('uploading'),
    'access arguments' => array('administer nodes'), // Users with administer nodes permission can access this form. Change it to a suitable one other than setting it TRUE blindly.
    'type' => MENU_CALLBACK,
  );
 
  return $items;
}
 
function upload_example_form() {
  $form['#attributes'] = array('enctype' => "multipart/form-data"); // Not necessary for D7.
  $form['upload'] = array(
    '#type' => 'file',
    '#title' => t('File'), // this is usually necessary.
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'), // t()
  );
 
  return $form;
}
 
function upload_example_form_validate($form, &$form_state) {
  if(!file_check_upload('upload')) {    
    form_set_error('upload', t('File missing for upload.')); // t()
  }
}
 
function upload_example_form_submit($form, &$form_state) {
    $validators = array();
    $file = file_save_upload('upload', $validators, file_directory_path());
    file_set_status($file, FILE_STATUS_PERMANENT);
    drupal_set_message(t('The form has been submitted.'));
}
?>

来自  http://stackoverflow.com/questions/14575157/drupal-6-file-upload-module

File upload empty in custom form


正确答案

function tag_graphics_form($form_state) {
    $form = array();
    $form['#attributes'] = array('enctype' => "multipart/form-data");
    $form['tag'] = array(
        '#required' => TRUE,
        '#type' => 'textfield',
        '#title' => t('Tag'),
        '#size' => 30,
      );
    $form['image'] = array(
        '#type' => 'file',
        '#title' => t('Image for tag page'),
        '#description' => 'Recommended width for image: 605px'
    );
    $form['description'] = array(
        '#type' => 'textarea',
        '#title' => t('Page Description for SEO')
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Save'
    );
    return $form;
}

function tag_graphics_form_validate($form, &$form_state){
  $field = 'image';

  $directory = file_directory_path();
  if (file_check_directory($directory, FILE_CREATE_DIRECTORY, $field)) {
    $validators = array();
    if ($file = file_save_upload($field, $validators, $directory)) {
      file_set_status($file, FILE_STATUS_PERMANENT);
      $form_state['storage']['file'] = $file;

    }
  }
}
function tag_graphics_form_submit(&$form, &$form_state){
  print_r($form_state['storage']['file']);
}

来自 http://drupal.stackexchange.com/questions/130152/file-upload-empty-in-custom-form

Howto: Drupal File Upload Form

正确答案

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_upload function:

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 #redirect form property to get your users to the next step. From there is really depends how you do things, what you'll need to do.

来自 http://stackoverflow.com/questions/1253094/howto-drupal-file-upload-form

普通分类: