I implemented a little hack that allows for multiple attachment upload. It is rather basic (e.g. no localization) and has not been thoroughly tested, but it seems to suit my needs.
Bear with me, as we will have to modify 5 different files.
(This hack is based on v1.0.6, but should work in newer versions, too)
1) common.js
add the following block of code:
Code: Select all
/*
* Class by Stickman -- http://www.the-stickman.com
*
* Mantis integration by [omeslo]
*/
function MultiSelector( list_target, max ){
// Where to write the list
this.list_target = list_target;
// How many elements?
this.count = 0;
// How many elements?
this.id = 0;
// Is there a maximum?
if( max ){
this.max = max;
} else {
this.max = -1;
};
/**
* Add a new file input element
*/
this.addElement = function( element ){
// Make sure it's a file input element
if( element.tagName == 'INPUT' && element.type == 'file' ){
// Element name -- what number am I?
element.name = 'file_' + this.id++;
// Add reference to this object
element.multi_selector = this;
// What to do when a file is selected
element.onchange = function(){
// New file input
var new_element = document.createElement( 'input' );
new_element.type = 'file';
// Add new element
this.parentNode.insertBefore( new_element, this );
// Apply 'update' to element
this.multi_selector.addElement( new_element );
// Update list
this.multi_selector.addListRow( this );
// Hide this: we can't use display:none because Safari doesn't like it
this.style.position = 'absolute';
this.style.left = '-1000px';
};
// If we've reached maximum number, disable input element
if( this.max != -1 && this.count >= this.max ){
element.disabled = true;
};
// File element counter
this.count++;
// Most recent element
this.current_element = element;
} else {
// This can only be applied to file input elements!
alert( 'Error: not a file input element' );
};
};
/**
* Add a new row to the list of files
*/
this.addListRow = function( element ){
// Row div
var new_row = document.createElement( 'div' );
// Delete button
var new_row_button = document.createElement( 'input' );
new_row_button.type = 'button';
new_row_button.value = 'Delete';
// References
new_row.element = element;
// Delete function
new_row_button.onclick= function(){
// Remove element from form
this.parentNode.element.parentNode.removeChild( this.parentNode.element );
// Remove this row from the list
this.parentNode.parentNode.removeChild( this.parentNode );
// Decrement counter
this.parentNode.element.multi_selector.count--;
// Re-enable input element (if it's disabled)
this.parentNode.element.multi_selector.current_element.disabled = false;
// Appease Safari
// without it Safari wants to reload the browser window
// which nixes your already queued uploads
return false;
};
// Set row value
new_row.innerHTML = element.value;
// Add button
new_row.appendChild( new_row_button );
// Add it to the list
this.list_target.appendChild( new_row );
};
};
replace
Code: Select all
<input name="file" type="file" size="40" />
Code: Select all
<input id="my_file_element" type="file" name="file_1" >
<div id="files_list"></div> <!-- queued files will be displayed here -->
<script>
var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 3 );
multi_selector.addElement( document.getElementById( 'my_file_element' ) );
</script>
3) bug_file_add.php
replace
Code: Select all
$f_file_error = ( isset( $f_file['error'] ) ) ? $f_file['error'] : 0;
file_add( $f_bug_id, $f_file['tmp_name'], $f_file['name'], $f_file['type'], 'bug', $f_file_error );
Code: Select all
$file_count = sizeof($_FILES);
$f_bug_id = $_POST['bug_id'];
// iterate through all upload fields ( = max uploads)
for ($i = 0; $i < $file_count; $i++) {
$thisfilename = "file_" . $i;
// check if file exists
if ( !is_blank( $_FILES[$thisfilename]['tmp_name'] ) && ( 0 < $_FILES[$thisfilename]['size'] ) ) {
$f_file_error = ( isset( $_FILES[$thisfilename]['error'] ) ) ? $_FILES[$thisfilename]['error'] : 0;
file_add( $f_bug_id, $_FILES[$thisfilename]['tmp_name'], $_FILES[$thisfilename]['name'], $_FILES[$thisfilename]['type'], 'bug', $f_file_error );
}
}
Code: Select all
$f_file = gpc_get_file( 'file' );
4) bug_report_page.php
replace
Code: Select all
<input tabindex="8" name="file" type="file" size="60" />
Code: Select all
<input id="my_file_element" type="file" name="file_1" >
<div id="files_list"></div> <!-- queued files will be displayed here -->
<script>
var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 3 );
multi_selector.addElement( document.getElementById( 'my_file_element' ) );
</script>
replace
Code: Select all
# Handle the file upload
if ( !is_blank( $f_file['tmp_name'] ) && ( 0 < $f_file['size'] ) ) {
$f_file_error = ( isset( $f_file['error'] ) ) ? $f_file['error'] : 0;
file_add( $t_bug_id, $f_file['tmp_name'], $f_file['name'], $f_file['type'], 'bug', $f_file_error );
}
Code: Select all
# Handle the file upload
$file_count = sizeof($_FILES);
// iterate through all upload fields ( = max uploads)
for ($i = 0; $i < $file_count; $i++) {
$thisfilename = "file_" . $i;
// check if file exists
if ( !is_blank( $_FILES[$thisfilename]['tmp_name'] ) && ( 0 < $_FILES[$thisfilename]['size'] ) ) {
$f_file_error = ( isset( $_FILES[$thisfilename]['error'] ) ) ? $_FILES[$thisfilename]['error'] : 0;
file_add( $t_bug_id, $_FILES[$thisfilename]['tmp_name'], $_FILES[$thisfilename]['name'], $_FILES[$thisfilename]['type'], 'bug', $f_file_error );
}
}
That should do it.
Most probably I made some typo's here and there, so if things aren't working out for you, please post a reply and I'll look into it.
Enjoy!
PS.
Credits go to 'Stickman' for writing the nice javascript, and giving me the idea. See the code comments for his website and the original script.