Hi,

I am sitting with a bit of a challenge here. I basically have two questions related to image uploads.

The first is uploading multiple images. Below is my code:

function competition_bookings_confirmation_page_two(&$form_state)
{
	$form = array();
	$form['#attributes'] = array('enctype' => "multipart/form-data");
	
	global $user;
	
	$g_maps = $form_state['storage']['page_one_values']['g_details']['g_maps'];
		
	for ( $i = 0; $i < $g_maps; $i++ )
	{
		$form['l_details'][$i] = array(
			'#type' => 'fieldset',
			'#title' => t('Match ' . ($i+1) . " Results"),
			'#collapsible' => TRUE,
			'#collapsed' => FALSE,
			'#tree' => TRUE,
		);
		
		$form['l_details'][$i]['e_id'] = array(
			'#type' => 'hidden',
			'#value' => $edit[0],
		);
		
		$form['l_details'][$i]['e_clan_1_score'] = array(
			'#title' => 'Enter your Teams Score',
			'#type' => 'textfield',
			'#value' => $edit[0],
		);
		
		$form['l_details'][$i]['picture_upload_' . $i] = array(
			'#type' => 'file', 
			'#title' => t('Match Screenshot'), 
			'#size' => 48, 
			'#description' => t('Your virtual face or picture. Maximum dimensions are %dimensions and the maximum size is %size kB.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'), '%size' => variable_get('user_picture_file_size', '30'))) .' '. variable_get('user_picture_guidelines', '')
		);
	}
	
	$form['#validate'][] = 'competition_bookings_score_confirmation_validate_picture';
	
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => 'Submit Score(s)',
	);
	
	return $form;
}

This part covers the input. The $g_maps variable sets how many blocks need to be created and also how many images need to be uploaded. The next block of code manages the resizing and saving to the database.

function competition_bookings_score_confirmation_validate_picture(&$form, &$form_state) 
{
	global $user;

	$validators = array(
		'file_validate_is_image' => array(),
		'file_validate_extensions' => array('png'),
	);

	for ( $i = 0; $i < $form_state['storage']['page_one_values']['g_details']['g_maps']; $i++ )
	{
		if ($file = file_save_upload('picture_upload_' . $i, $validators))
		{			
			$info = image_get_info($file->filepath);
			
			$tmpName  = $file->filepath;

			switch ($info['mime_type']) 
			{
				case 'image/png':
					$old_image = imagecreatefrompng($tmpName);
					$current_width = imagesx($old_image);
					$current_height = imagesy($old_image);
					
					$width = 158;
					$height = 158;
					
					$image_p = imagecreatetruecolor($width, $height);
					imagecopyresampled($image_p, $old_image, 0, 0, 0, 0, $width, $height, $current_width, $current_height);
					
					imagejpeg($image_p, $tmpName);
					$fp      = fopen($tmpName, 'rb');
					$content = '';
					while (!feof($fp)) 
					{
						$content .= fread($fp, 8192);
					}
					$content = mysql_escape_string($content);
					fclose($fp);
					break;
			}

			$query = "INSERT INTO cb_events_score (es_id, es_clan, es_image) VALUES (" . $form_state['storage']['page_one_values']['g_details']['e_id'] . ", " . 1 . ", '" . $content . "')";
			mysql_query($query) or die("Query failed : " . mysql_error());
		}
	}
}

now my first question is, how do I get the field name, this is the name that is generated from "$form['l_details'][$i]['picture_upload' . $i]". What would the $source value for file_save_upload need to be in this case? Any suggestions?

My second question is, I want to upload the image to a blob in my mysql database. I have been using db_query for everything but %b doesn't appear to want to work. The blob data keeps getting inserted into the database corrupted.

Any assistance will be greatly appreciated.

Comments

neochief’s picture

You need to put correct name to get correct structure of FILES array. Try this:

$form['l_details'][$i]['picture_upload_' . $i] = array(
  ...
  '#name' => 'files['. $i .']',
  ...
);

After this you can put '1' in $source to get second picture.