1779 Posts in 582 Topics by 556 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 372 Views |
-
[SOLVED] Saving uploaded file from front-end into assets/database & has_many relation

16 January 2013 at 10:27am
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(); -
Re: [SOLVED] Saving uploaded file from front-end into assets/database & has_many relation

17 January 2013 at 2:47am
OK, so as it turns out, the files get auto-saved as well upon $form->saveinto(), causing the wrong Filename & link issue. I managed to fix this by saving the files manualy & adding them into the has_many relation, and then saving the rest of the form fields into the datarecord, filtering out the uploadfields.
| 372 Views | ||
|
Page:
1
|
Go to Top |

