I'm creating a module uses a custom content type (created through the module, not CCK) that needs to be able to upload images and get information from that image to associate with the node. I know you're gonna say it would make my life easier if I just used CCK or the Image module or something, but I really need the image uploading mechanism to be independent of other modules for this module to do what it needs to do.

I know how to make an upload field using hook_form(), but where to go from there? This is what I have for the form hook.


function poster_form(&$node) {
	$type = node_get_types('type', $node);

	if ($type->has_title) {
		$form['title'] = array(
			'#type' => 'textfield',
			'#title' => check_plain($type->title_label),
			'#required' => TRUE,
			'#default_value' => $node->title,
			'#weight' => -5,
		);
	}
	$form['poster']['#attributes'] = array('enctype' => "multipart/form-data");
	$form['poster'] = array(
		'#type' => 'file',
		'#title' => t('Upload your picture'),
	);
	$form['#submit'] = array('upload_form_submit');
	
	return $form;
}

I just don't really know where to go from there. How do I take the information from the file upload field, upload it to the files directory, and get other information I want from the uploaded image. I understand you're supposed to use a callback function for the submit field, which I've set to upload_form_submit. What exactly is supposed to be in this function that will make the upload form do magical things?

Comments

oligoelemento’s picture

I'm interested in solving the same problem.

archard’s picture

Yes, you have to use the file_save_upload() function in your validate/submit callback. It will return a file object which you can then manipulate.

jesusDrupal’s picture


	$validators = array();
	$destino = file_directory_path();
  	if ($file = file_save_upload('upload', $validators, $destino)) {
		$destino = file_directory_path() . '/imagenes/actvproys/';
		file_move($file, $destino, FILE_EXISTS_REPLACE);

In my case i uploaded the image and then i moved it to another location because Drupal by default stores
the images in sites/default/files. So i created a folder inside 'files' where i store my images. If i tried to
store them out of 'files' folder i couldn't store them. I don't know the reason.

Hope this help you!!!