Drupal 8 supports multiple file upload, however the native interface requires three steps:
- Select one or more files
- Click Upload
- Click Save
Ideally this could be simplified to a single step i.e. select one of more files, once done it uploads them. The user can delete a file later if they wish, but no point showing them the list of which they just select, just to make them change their mind.
How should one click upload for multiple files be implement in Drupal?
In pure PHP this can be done easily as follows (but Drupal 8 adds too many constraints for this to work):
<html>
<body>
<?php
$upload_dir = '/tmp/upload_test';
if ( ! is_dir( $upload_dir ) )
{
mkdir( $upload_dir, 0777, true );
}
/** Rearrange the "parallel" _FILES array into consolidated file items. */
function rearrange( $arr )
{
foreach( $arr as $key => $all )
{
foreach( $all as $i => $val )
{
$new[$i][$key] = $val;
}
}
return $new;
}
if ( ! empty( $_FILES['data_fs_uploads'] ) )
{
$files = rearrange( $_FILES['data_fs_uploads'] );
print '<p>Copying uploaded files...</p>';
foreach ( $files as &$file )
{
// File has 'name', mime 'type', 'tmp_name' path, 'error', and 'size'.
$file['ext'] = pathinfo( $file['name'], PATHINFO_EXTENSION );
$target_file = $upload_dir . '/' . $file['name'];
if ( move_uploaded_file( $file["tmp_name"], $target_file ) )
{
print '<p>Copied ' . $file['name'] . '</p>';
}
else
{
print '<p>FAILED to copy ' . $file['name'] . '</p>';
}
}
}
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input id="data_fs_uploads" type="file" name="data_fs_uploads[]" multiple="multiple" onchange="javascript:this.form.submit();" />
</form>
<?php
foreach ( glob("$upload_dir/*") as $file )
{
print '<div>' . $file . '</div>';
}
?>
</body>
</html>