0

Im currently developing a module that must have a capability to browse/select and upload an image to the server.

function myid_user_page_form(){ 

    $form = array();

    $form['id']['myphoto'] = array(
        '#type' => 'item',    
        '#markup' => '<div id="div_myphoto"><img id="img_myphoto" src="image.png"/>'
    );

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

function formsave_action($form,&$form_state) {  

}

How will I upload an image(#img_myphoto) to my server?

  • Hey, My answer provides you all the functionalities you need to manage (upload / remove ) image in Drupal Environment – Himanshu Pathak Mar 23 '15 at 8:52
1

Try to implement following code

function myid_user_page_form(){ 
$form = array();
$form['myphoto'] = array(
  '#type' => 'managed_file',
  '#title' => 'File Upload',
  '#upload_validators' => array('file_validate_extensions' => array('png gif jpg jpeg')),
  '#upload_location' => 'public://my-folder/'
   );

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





function formsave_action($form,&$form_state) { 
    $file = file_load($form_state['values']['myphoto']);
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);
}
  • Thanks, but I just wanted to save the image shown in "#img_myphoto". How will I do that without using a managed_file? – user34776 Mar 23 '15 at 7:18
0

You need to add managed file field as shown in the example below below:

<?php
// Use the #managed_file FAPI element to upload an image file.
$form['image_example_image_fid'] = array(
  '#title' => t('Image'),
  '#type' => 'managed_file',
  '#description' => t('The uploaded image will be displayed on this page using the image style choosen below.'),
  '#default_value' => variable_get('image_example_image_fid', ''),
  '#upload_location' => 'public://image_example_images/',
);
?>
  • Thanks, but I just wanted to save the image shown in "#img_myphoto". How will I do that without using a managed file? – user34776 Mar 23 '15 at 7:14
0
//Implements whole image upload functionality

// - See the hook menu first
// - I have used thing "YOURMODULE" you should replace it accordingly
// - This will show you whole functionality of managing (adding / removing ) of a file in drupal environment.

/**
 * Implements hook_menu().
 */
function YOURMODULE_menu() {
  $items = array();

  $items['yourmodule/image-upload'] = array(
    'title' => 'Image Upload Functionality',
    'description' => 'Here you can try uploading image',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_YOURMODULE_image_upload'),
    'access arguments' => array('access content'),
  );

  return $items;
}



/**
 * YOURMODULE Module showing image upload functionality.
 */
function _YOURMODULE_image_upload($form, &$form_state) {
  $form['description'] = array(
    '#type' => 'markup',
    '#markup' => '<p><strong>' . t('See image upload in action') . '</strong></p>',
  );

  if ($image_fid = variable_get('yourmodule_image_fid', FALSE)) {
    $image = file_load($image_fid);
    $form['image'] = array(
      '#markup' => theme('image_style', array('path' => $image->uri, 'style_name' => 'thumbnail')),
    );
  }

  $form['yourmodule_image_fid'] = array(
    '#type' => 'managed_file',
    '#title' => t('Image'),
    '#description' => t('Please upload an image.'),
    '#default_value' => variable_get('yourmodule_image_fid', ''),
    '#upload_validators' => array('file_validate_extensions' => array('png gif jpg jpeg')),
    '#upload_location' => 'public://yourmodule_uploads',
  );

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


/**
 * Function to save uploaded image in YOURMODULE Module.
 */
function _YOURMODULE_image_upload_submit($form, &$form_state) {
  if ($form_state['values']['yourmodule_image_fid'] != 0) {
    $file = file_load($form_state['values']['yourmodule_image_fid']);
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);

    // When a module is managing a file, it must manage the usage count.
    // Here we increment the usage count with file_usage_add().
    file_usage_add($file, 'yourmodule', 'sample_image', 1);

    variable_set('yourmodule_image_fid', $file->fid);
    drupal_set_message(t('Your image named @name has been uploaded successfully', array('@name' => $file->filename)));
  }
  elseif ($form_state['values']['yourmodule_image_fid'] == 0) {
    $fid = variable_get('yourmodule_image_fid', FALSE);
    $file = $fid ? file_load($fid) : FALSE;
    if ($file) {
      // Delete the file use
      file_usage_delete($file, 'yourmodule', 'sample_image', 1);
      file_delete($file);
    }
    variable_set('yourmodule_image_fid', FALSE);
    drupal_set_message(t('You image named @name has been removed successfully', array('@name' => $file->filename)));
  }
}

Your Answer

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