I am trying to setup a scenario where a user might be uploading multiple files (although 1 at a time). For some reason, I am not able to figure out how to accomplish this. I have added field elements to my form that allow single file uploads. As a user, I can easily get to uploading 1 file and then previewing it. But when I go to add another file, the previous file still shows as being uploaded and if I try to replace it with another one, it says the new file will delete the old one and I also get a DB error -

user warning: Incorrect integer value: '' for column 'weight' at row 1 query: UPDATE upapi_data SET description='B_First Image.jpg', weight='', fid=2, obj_id=0, obj_type='node', label='mri_upload' WHERE upapi_id=2 in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\drupal\includes\database.mysqli.inc on line 151.

Any pointers?

Thanks!

Comments

Dave Cohen’s picture

Clearly, the 'weight' used to sort multiple files is not being set. So there is a bug to track down.

Can you reproduce this problem using the upload_example.module?

Can you post your form element here?

achatter’s picture

I just now tried the upAPI example by adding two stories to the nodes and I didn't get the error. I also didn't get the scenario where while creating a new Story, the old file was showing up. Obviously, I have not created my upfield elements correctly. I have pasted a section of my module that has the upfield elements - if it doesn't make sense, I will attach the module.

/**
* Implementation of hook_form().
*/
function patient_details_form(&$node) {
  $type = node_get_types('type', $node);

  // We need to define form elements for the node's title and body.
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t("Last Name"),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => 0
	
  );
  $form['firstname'] = array(
    '#type' => 'textfield',
    '#title' => t("First Name"),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => 2
  );
  
  $form['ssn'] = array(
  	'#type' => 'textfield',
	'#title' => t("SSN #"),
	'#required' => TRUE,
	'#default_value' => 'xxx-xx-xxxx',
	'#weight' => 3
  );
  
  $form['dob'] = array(
  	'#type' => 'textfield',
	'#title' => t("Date of Birth"),
	'#required' => TRUE,
	'#default_value' => 'mm-dd-yyyy',
	'#weight' => 3
  );
  
  $form['Images'] = array(
  	'#title' => t('Tests Conducted'),
	'#type' => 'fieldset',
	'#collapsible' => FALSE,
	'#weight' => 4
  );
  
  
  $form['image_options'] = array(
  	'#type' => 'value',
	'#value' => array(t('No'),t('Yes'))
  );

  $form['Images']['mri'] = array(
  	'#title' => t('Was MRI Conducted?'),
	'#type' => 'select',
	'#description' => t('Please select Yes or No to save if MRI was conducted'),
	'#options' => $form['image_options']['#value']
  );

$form['Images']['mri_file'] = array(
  	'#type' => 'upfield',
	'#label' => 'mri_upload',
	'#multiple' => FALSE,
	'#title' => t('MRI Image Upload'),
	'#description' => t('If Yes above, please enter filename to upload file'),
	'#upfield_action' => 'upapi_callback',
	'#upfield_action_data' => array('obj_type' => UPAPI_TYPE_NODE_ID,
									'obj_id' => $form['nid']['#value'],
									'destination' => '%file_directory/mri/%uid/%filename')
  );

  return $form;
}

And here is the code for the nodeapi -

/**
* Implementation of hook_nodeapi
*/

function patient_details_nodeapi(&$node,$op,$a3,$a4=NULL){
if ($op == 'view') {
    foreach (upapi_load(array('obj_type' => UPAPI_TYPE_NODE_ID,
                              'obj_id' => $node->nid)) as $file) {
      $label = $file->label;
      $output = '<dt>.$label</dt><dd>'.l($file->description, 
                                            upapi_download_url($file,FALSE,FALSE));
      //$output .= dpr($file, 1); // debug
      $output .='</dd>';
	  }
  }
}

I really appreciate you looking into this for me. I have been struggling to get this to work!

Thanks.
Arindam.

achatter’s picture

I copied over the upapi_example files and it seems to have fixed that issue. I can now upload a new file everytime a new node is created.

The last bit of problem I am facing is showing a link to the file so it can be pulled up from the location where it is stored. In my upfield element I am uploading a file to a different location than the upapi_example. When I get the link back after the file is uploaded, I get a "Access Denied" message. I am not able to follow how the link is getting set. When I look at the link it is something like - http://locahost/drupal/?q=upapi/download/i where i is a number. It works in the Story node when I upload a file, and I can click on the link created and it pulls up the file using the upapi_example module, but not from my node/module. I am tearing my hair over this!!! Must be something simple I am overlooking.

Any advice?

Thanks.

achatter’s picture

Status: Active » Closed (fixed)

I was not assigning rights for download to the particular labels that were getting the file uploads. Once done as described in upapi_example, everything was fixed and it works like a charm!

So in detail -

function upapi_example_upapi(&$file, $op, &$a3) {
  //drupal_set_message("upapi_example_upapi($op)"); // debug

  if ($op == UPAPI_OP_MAY_DOWNLOAD) {
    // UpAPI is asking whether the current user has permission to download the
    // file.  A smarter module might have some logic here.  All well behaved
    // modules will leave $a3 alone - do not change it - unless they know for
    // certain the user is allowed.  The policy is that the file cannot be
    // downloaded unless a module says it can.  So modules should only modify
    // $a3 if the file is one they are responsible for.
    if ($file->label == 'example_single' || $file->label == 'example_multi')
      // Allow download.
      $a3 = TRUE;
  }
}

$file->label needs to have your file labels set to allow download and viewing of the files/images etc.

Thanks for the wonderful module. It has been very helpful.