I will try to post the solution tonight. Sorry for inconvenience!



A multiple input requires brackets after name as in below (copying from limitless kit). So we can't use standard couch-powered inputs yet.
<input type='file' name='multi[]' accept='image/*' id="multi" class="file-input-advanced" multiple="multiple" data-show-preview="false" />
I'm posting a sample editable for multi-uploading images, so paste this in template. If you need any other files - that's easy to change with html-standard "accept" filter.
- Code: Select all
<cms:repeat '3' startcount='0' >
<cms:editable name="img_<cms:show k_count />"
required='0'
allowed_ext='jpg, jpeg, png, gif'
max_size='20000'
type='securefile'
label='Bilde'
desc="<cms:add k_count '1' />"
width='200'
height='200'
crop='1'
quality='90'
order="100<cms:show k_count />"
/>
</cms:repeat>
1. I hope editable definition is straightforward, as it closely follows the one in couch docs. The only interesting part is dynamic creation of 3 editables by repeating them automatically. Kinda saves copypasting, but won't hurt if you have all of them typed in separately, of course. Just saves the hassle if there are 20 possible files to upload..
So, we gonna let user upload 3 files at once -> then the form submits ->then files go into the prepared nest.
A sample button to call the modal:
- Code: Select all
<a href="#" data-toggle="modal" data-target="#add" >
<button type="button" class="btn btn-default"><i class="icon-plus3"></i></button>
</a>
A sample of complete HTML for the modal, including the code that will be explained below:
- Code: Select all
<cms:set my_template = 'your_template.php' scope='global' />
<div id="add" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header bg-info">
<button type="button" class="close" data-dismiss="modal">×</button>
<h5 class="modal-title">Create new record..</h5>
</div>
<cms:form
name='create_record'
masterpage=my_template
mode='create'
enctype='multipart/form-data'
method='post'
anchor='0'
>
<cms:if k_success >
<cms:db_persist_form
_invalidate_cache='1'
_auto_title="1"
/>
<cms:if k_success >
<cms:set_flash name='success' value='1' />
<cms:redirect url="<cms:link masterpage=my_template />" />
</cms:if>
</cms:if>
<cms:if k_error >
<cms:set_flash name='alert' value='1' />
</cms:if>
<div class="modal-body">
<div class="row">
<div class="col-md-12">
<legend class="text-bold">Add up to 3 pictures</legend>
<div class="form-group">
<input type='hidden' name='input_multiple' value='multi' />
<input type='hidden' name='securefile_mask' value='img_' />
<input type='hidden' name='submit' />
<input type='file' name='multi[]' accept='image/*' id="multi" class="file-input-advanced" multiple="multiple" data-show-preview="false" />
<cms:hide>
<cms:repeat '3' startcount='0' >
<cms:input type='bound' name="img_<cms:show k_count />" />
</cms:repeat>
</cms:hide>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-link" data-dismiss="modal">Dismiss</button>
<button type="submit" name='submit' class="btn btn-primary">Create<i class="icon-arrow-right14 position-right"></i></button>
</div>
</cms:form>
</div>
</div>
</div>
Extra code explanation.
1. We gonna indicate the name of our html input, which will be used: <input type='hidden' name='input_multiple' value='multi' />
2. We gonna indicate the name prefix of the editable that we defined: <input type='hidden' name='securefile_mask' value='img_' />. If your editables will be called as file_1, file_2 etc, then change to value='file_'
3. Indicate that form is submitted. Used to make it all work <input type='hidden' name='submit' />.
4. Next, goes our multiple input.
5. Following is a hidden part, that is necessary to create hidden spots for "data-bound" inputs. These are couchcms system inputs that are mandatory for each bound input. Since we have defined our own input for multi-selection of images, then we gonna hide bound inputs, but still those are mandatory, as representatives of all our defined editables.
So, if there are 20 files to upload max at once, there must be 20 hidden bound inputs. This is handled by changing the number in cms:repeat, just the same trick as in defining editables.. OF course name prefix must match.
- Code: Select all
<cms:hide>
<cms:repeat '3' startcount='0' >
<cms:input type='bound' name="img_<cms:show k_count />" />
</cms:repeat>
</cms:hide>
That's it for the HTML part.
Now php part.
When the form submits, we must take the flow into our hands before CouchCMS gets to it.
Paste a line above all lines in the template. We gonna call our silly php script.
- Code: Select all
<?php include 'upload-multi-basic.php'; ?>
<?php require_once( 'cms/cms.php' ); ?>
Here is what we need to do: we must reformat the way our files got submitted by default into the way Couch understands them.
Regular HTML form submits the file and PHP by itself arranges the uploading to a temp folder and does everything that we don't need to touch. It normally puts info to its array $_FILES. We can see what's inside of POST and FILES by logging like this:
- Code: Select all
error_log( print_r($_POST, true));
error_log( print_r($_FILES, true));
Nothing fancy, but we can check the FILES array, which at the start looks like this for a sample png file:
- Code: Select all
[multi] => Array
(
[name] => Array
(
[0] => 2017-05-19-153118.png
)
[type] => Array
(
[0] => image/png
)
[tmp_name] => Array
(
[0] => D:\CloudOne\OpenServer\userdata\temp\phpCB41.tmp
)
[error] => Array
(
[0] => 0
)
[size] => Array
(
[0] => 10745
)
)
But for Couch we must make it look like this:
- Code: Select all
[f_img_0] => Array
(
[name] => 2017-05-19-153118.png
[type] => image/png
[tmp_name] => D:\CloudOne\OpenServer\userdata\temp\phpCB41.tmp
[error] => 0
[size] => 10745
)
Having done that, we are all set. So just by changing the look of array we can make uploading work.

Create and empty file "input-multi-basic.php". It will have this code and nothing else:
- Code: Select all
<?php
if( !isset( $_POST['input_multiple'] ) || !isset( $_POST['securefile_mask'] ) ) return;
if( isset( $_FILES[$_POST['input_multiple']] ) ){
// Files submitted via multiple input.
$uploaded = $_FILES[$_POST['input_multiple']];
$mask = $_POST['securefile_mask'];
// Count # of uploaded files
$total = count( $uploaded['name'] );
$available_spots = array();
// Loop through existing images to build array of empty spots
for( $i=0; $i<20; $i++ ){
if( !isset( $_POST['secure_file_id_f_' . $mask . $i] ) ){
$available_spots[] = $i;
}
}
$total_spots = count( $available_spots );
if( $total_spots < $total ){
$warning = $total_spots;
$total = $total_spots;
}
// Loop through uploaded images
for( $i=0; $i<$total; $i++ ){
$empty_spot = $available_spots[$i];
//Get file info
$tmpFilePath = $uploaded['tmp_name'][$i];
$tmpFileName = $uploaded['name'][$i];
$tmpFileMime = $uploaded['type'][$i];
$tmpFileSize = $uploaded['size'][$i];
$tmpFileErro = $uploaded['error'][$i];
//Make sure we have a filepath, so file is uploaded to server okay
if( $tmpFilePath && !$tmpFileErro ){
// make sure format matches the expected by CouchCMS
$_FILES['f_' . $mask . $empty_spot] = array(
'name' => $tmpFileName,
'type' => $tmpFileMime,
'tmp_name' => $tmpFilePath,
'error' => '0',
'size' => $tmpFileSize );
}
}
}
It has a limitation of max 20 uploaded files at once, because I believe it's what PHP has by default as a limit after default installation.
Now it all should work fine. Create a template, list with cms:pages its cloned pages in list-view.
Then form gets submitted with or without attachments and a cloned page gets created in the background. After successful action, there is a redirection in the form again to the list-view, so you can output your images like this:
- Code: Select all
<cms:pages masterpage=my_template >
<h5>Page#<cms:show k_current_record /> @ <cms:show k_page_date /></h5>
<p>Attachments:
<cms:repeat '3' startcount='0' >
<cms:show_securefile "img_<cms:show k_count />" >
<div class="thumbnail">
<div class="thumb">
<img src="<cms:cloak_url link=file_id />" />
</div>
</div>
</cms:show_securefile>
</cms:repeat>
</cms:pages>
Please report any bugs. This is from real project, so I hope it's working fine for you as well as it did for me.
Don't thank me

