0

I am developing a custom module in which I want to:

  1. Upload an image through a form.
  2. Move image from default folder to my desired folder.
  3. And finally display the image under the same form (below the submit button).

I've achieved the first goal by implementing hook_menu and hook_form but I am stucked very badly in the remaining two goals for last 3 days. Whenever I try to move uploaded image from default folder I get the error message of "Invalid location" and for the third point I didn't understand what to do? I want when the user selects the image and submits the form the image is displayed on the same page under the submit button. Any help would be really appreciated. Here's my code:-

function create_ad_form($form, &$form_submit)
{
   ...

   $form['image_file'] = array(
     '#title' => t('Upload Banner:'),
     '#type' => 'file'
   );

   $form['#attributes']['enctype'] = 'multipart/form-data';

   ...
}

function create_ad_form_submit($form, &$form_state)
{
    $module=drupal_get_path('module', 'create_ad');
    $validators = array();

    $file = file_save_upload('image_file', $validators,"public://",FILE_EXISTS_RENAME);

     if ($file)
 {
    $file->status=FILE_STATUS_PERMANENT;
    file_save($file);

    $result = file_unmanaged_copy($file, $module, FILE_EXISTS_RENAME);
    if ($result == 1)
    {


    }
    else
    {
      drupal_set_message('Couldn\'t copy file: '.$file->name);
    }
  }
  else
  {
    form_set_error('create_ad', t("Failed to save the file."));
  }
}
Muhammad Qasim
  • 351
  • 1
  • 6
  • 21

1 Answers1

0

I've managed to display the image, however still can't move the image to any folder so placed them in the default "public://" directory. Display image on the same page without ajax is discussed here: Drupal 7 FAPI- Adding form elements in validate or submit handlers and with ajax discussed here: Drupal 7 FAPI - ajax image preview

Community
  • 1
  • 1
Muhammad Qasim
  • 351
  • 1
  • 6
  • 21