Jump to content

multiple input file types in a single form


johnmerlino

Recommended Posts

Hey all,

 

I am building a simple cms. I have a posts table and I have an images table. A post has many images and images has a foreign key to the posts table. So when a user edits, updates, creates, and deletes a post, they affect the images related to the post. Sometimes a post can have more than one image, like three images. Hence I rendered this in the view (note that I am using a datamapper that converts tables to objects and fields to key/value pairs):

 

foreach($records as $post){
	echo form_open_multipart("homes/update/$post->id"); //File uploads require a multipart form. Default form handling uses the application/x-www-form-urlencoded content type. Multipart forms use the multipart/form-data encoding. //this is critical to pass the id as part of the action attribute of the form, so we can use our params hash to target the id to update the specific record

		echo label('Update Title');
	echo form_input('title',$post->title);
	echo label('Update Body');
	echo form_textarea('body',$post->body);

		$images = $post->images->include_join_fields()->get();
	if(!is_null($images->image_file_name)){  
			echo label('Update Images');		
		foreach($images as $image){
			echo form_upload('image_file_name',$image->image_file_name);  
		}
	}

}
echo form_submit('submit','Update');

The above line of code will render a few input file types. The problem occurs during posting to my update method. It is looking for one parameter from the input file field and so if I upload three different images, it will only look for one and write only one to database:

 

$field_name = 'image_file_name';
			if ( ! $this->upload->do_upload($field_name)){
				$error = array('error' => $this->upload->display_errors());
			echo $error['error'];
		//	redirect('homes/edit');
  			}
		else {
				$data = array('upload_data' => $this->upload->data());
			$image_file_name = $data['upload_data']['file_name'];

 

Is it possible to do wht I am trying to do? Should I only have on input file type per form submission or is that I need to fix the code to accomodate for multiple submissions by creating an array of sorts?

 

Thanks for response.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.