Hi, I have a front-end form which allows attaching multiple files:
public function myform(){
...
$fields->insertBefore($att=new FileField("Attachments[]", "Attachments"), "Title");
$att->setFolderName('fileattachments');
...
}
I save the uploaded files into the record in this function:
public function project_save($data, $form){
...
// save file attachments into record;
$i = 0;
while(isset($data['Attachments']['tmp_name'][$i])) {
if (!empty($data['Attachments']['name'][$i])) {
// create new single file array from file uploads array
$file = array();
$file['name'] = $data['Attachments']['name'][$i];
$file['type'] = $data['Attachments']['type'][$i];
$file['tmp_name'] = $data['Attachments']['tmp_name'][$i];
$file['error'] = $data['Attachments']['error'][$i];
$file['size'] = $data['Attachments']['size'][$i];
// create & write uploaded file in DB
try {
$fileClass = File::get_class_for_file_extension(pathinfo($file['name'], PATHINFO_EXTENSION));
$newfile = new $fileClass();
$upload = new Upload();
// get folder from form upload field
$folder = $form->Fields()->fieldByName('Attachments[]')->getFolderName();
$folderObj = Folder::find_or_make($folder);
$upload->loadIntoFile($file, $newfile, $folder);
$fileObj = $upload->getFile();
// for some reason the Filename is off...
// $fileObj->setFilename( "assets/$folder/".$fileObj->Name );
// $fileObj->setParentID( $folderObj->ID );
// $fileObj->write();
$this->Record->Attachments()->add($fileObj);
} catch(ValidationException $e) {
$form->sessionMessage('Extension not allowed...','bad');
return $this->redirectBack();
}
}
$i++;
}
$this->Record->write();
...
}
This works OK, the relation gets added. The problem is that the resulting File/Image object has the wrong Filename & ParentID (folder ID) set. The actual files do get saved into the right folder ('fileattachments'), but the database record for the file always says it's in assets/Uploads/file.jpg instead.
I guess the problem's in Upload::loadIntoFile(), but I cannot seem to figure out what's going wrong;
$upload->loadIntoFile($file, $newfile, $folder);
$fileObj = $upload->getFile();