0

I am creating a custom module where the user uploads and image to the Drupal DB using a form. All the data except for the file is being put into the DB though. The only thing that shows in the 'image' field is "[BLOB - 0 B]" The only instructions/tutorials that I can find are for uploading files to a directory, not a table in the db.

function new_floor_map_form($form, &$form_state) {
    $form['new'] = array(
        '#type'     => 'markup',
        '#markup'   => '<h2>'.t('Start a new floor map:').'</h2>',
    );
    $form['#attributes']['enctype'] = 'multipart/form-data';
    $form['file'] = array(
        '#type'     => 'file',
        '#title'        => t('Choose a file'),
    );
    $form ['name'] =array(
        '#type'     => 'textfield',
        '#title'        => t('Name:'),
    );
    $form['active'] = array(
        '#type'     => 'checkbox',
        '#title'        => t('Set as current floor map?'),
    );
    $form['submit'] = array(
        '#type'     => 'submit',
        '#value'    => t('Submit'),
    );

    return $form;
}
function new_floor_map_form_submit($form,&$form_state) {
    if(isset($form['file']) && (!empty($form['name']))){
        $insert = db_insert('floor_map_images')
            ->fields(array (
            'image' => $form_state['values']['file'],
            'name'      => $form_state['values']['name'],
            'active'        => $form_state['values']['active'],
            ))
            ->execute();
    }

 }
2

You shoud use Drupal file managing system. When Drupal is aware of a file, its data is stored in the file related tables. If you need to do something with a file you only need its identifier, called fid.

So, in your form upload the file using Drupal Form API, and store the fid in your table. Whenever you need to do something with the file use the fid to get all the file information.

This question may be very interesing for you: Managed file: the file used in the field may not be referenced

Check also the file_managed Form API element: https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#managed_file

| improve this answer | |
  • OK, I'm new this, right now, my module hasn't created any nodes. So, in the install file, I'm creating one. My next question is, can I make one single node to hold all the images that are uploaded using my module? Only one image will be used at a time, if that matters. – slpcc63 Mar 5 '14 at 14:22
  • 1
    That is a completely different question, ask a new one. But is not a good approach to create a node to hold all images. in your new question explain what problem you have, what you want to achieve and ask for Drupal ways to solve it. – sanzante Mar 5 '14 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.