Search code examples
phpfile-uploaddrupaldrupal-7drupal-modules

Writing Drupal Module with File Upload


I'm creating a Drupal module for upload hero unit type banners. Basically, I want to be able to select a file, and along with that file, enter a name of one of the sections on the website to associate that banner with. So for example, if I select a file and put the word "shop" in an input field, this image should be uploaded and when I go to my website "/shop" section, I want to see this image.

I've followed this tutorial, https://api.drupal.org/api/examples/form_example!form_example_tutorial.inc/function/form_example_tutorial_10/7.x-1.x, and have file uploading working correctly.

However, I am very new to Drupal and wondering the best way to approach showing my uploaded hero images in the Drupal module. At the same time, I want to be able to access my uploaded images from anywhere within my Drupal application so that I can display the images.

Would a database be needed or some use of the variable_get?


Solution

  • Given your description what you want, that can be achieved with the CCK + Views with contextual filters relative easily.

    If you really want to use your own module you'd have to add a table which stores the file uri along with the additional information the user provides in the form. The URI - following the tutorial - you can get with

    $uri = $file->uri
    

    after the file_save($file) command.

    The variable_set and variable_get are meant for settings like in which mode the module should operate and not for storing data.