1
votes

I found multiple drupal and stackoverflow pages with examples of upload forms, but I wasn't able to get one to work. here I have included all the code I used, sort of parsing together what other people have done. I have included a menu, a form, a submit, and validate function. The upload file doesn't save in the sites --> default --> file folder, and the submit set_message isn't displayed upon submit. Why doesn't this work?

<?php

function upload_example_menu() {
  //$items = array();
  $items['upload_example'] = array(
    'title' => t('Upload'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('upload_example_form'),
    'description' => t('uploading'),
    "access callback" => TRUE,
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function upload_example_form() {
  $form['#attributes'] = array('enctype' => "multipart/form-data");
  $form['upload'] = array('#type' => 'file');
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );

  return $form;
}

function upload_example_form_validate($form, &$form_state) {
  if(!file_check_upload('upload')) {    
    form_set_error('upload', 'File missing for upload.');
  }
}

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.'));
}
?>
1
I suggest you to see hook_menu example to see how you should use drupal_get_form with a page arguments array to show the form. You get the white screen because you define the same function twice. hook_menu example page has a good example. Also, you will find perfect examples in drupal.org/project/examples - AKS
The examples page does not have an example for file upload in drupal 6, i think. - ingrid
i edited that error, i wrote the function name out wrong and called it _menu twice. There is still a problem. - ingrid
May be you need to tell us what is the exact problem. If it's a blank screen, enable error reporting to E_ALL and you will see the error message. Search drupal.org for "wsod" and you will see how (I'm on my mobile so can't do that fr you with ease) - AKS
And by the way, your upload_example_form() will receive $form and $form_state both just like submit and validate functions. - AKS

1 Answers

2
votes

Sorry I had many comments added to the answer but could not collect them into a reply like this.

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.'));
}
?>

Let us know how it went :)