This is the form in my template:
<form action="" method="post" enctype="multipart/form-data">
<ul>
<li>
<label for="file">Pictures</label>
<input name="file[]" type="file" multiple>
</li>
<input type="submit" name="submit" value="Send it in!" class="button">
</ul>
</form>
This is my controller now:
<?php
return function($site, $pages, $page) {
$error = null;
if(r::method() === 'POST') {
// The form has been sent
$title = get('brand') . "-" . get('tube-name');
// Build an array of the data you want to allow as fields
// get() fetches the form field value with that `name`
$data = array(
'tube-name' => get('tube-name'),
'brand' => get('brand'),
'different_manufacturer' => get('different_manufacturer'),
'country' => get('country'),
'code_on_tube' => get('code_on_tube'),
'factory_code' => get('factory_code'),
'year' => get('year'),
'bottle_height' => get('bottle_height'),
'Tip-Shape' => get('Tip-Shape'),
'glass_color' => get('glass_color'),
'Mica' => get('Mica'),
'getter' => get('getter'),
'getter_support' => get('getter_support'),
'plate_look' => get('plate_look'),
'plate_size' => get('plate_size'),
'plate_color' => get('plate_color'),
'pin_color' => get('pin_color'),
'base' => get('base'),
'special_use' => get('special_use'),
'other_marks' => get('other_marks'),
'notes' => get('notes'),
'uploader_name' => get('uploader_name'),
'uploader_email' => get('uploader_email')
);
// Create a new page as child of the current page
// You can also use a different page by using `page('whatever')->children()->create()`
$p = page('uploads')->children()->create($title, 'tube', $data);
// Upload an image
$files = $_FILES['file'];
foreach($files as $file) {
try {
$upload = new Upload($p->root() . DS . '{safeFilename}', array('input' => 'file'));
} catch(Error $e) {
switch($e->getCode()) {
case Upload::ERROR_MISSING_FILE:
// File does not exist
$error = 'No file uploaded.';
break;
// See the Upload class for other error values
}
}
}
}
return compact('error');
};
?>