Carey Estes
Carey Estes

Reputation: 1564

Uploading a file with custom generated symfony 1.4 forms

I use a module that creates customized html forms through template partails

I need to manage 4 file uploads of a form.

In actions I pull all field data like so:

$allData = $request->getParameterHolder()->getAll();

which would get me the uploaded file name, but I need to also save that file to the uploads dir.

I am trying to bind the form like so:

if( isset($customData['getAttachments']) && count($allData)){
        $attachments = $formInput->bind($request->getParameter('forms_input'), $request->getFiles('forms_input'));
}

which succesfully generates the $attachments object. Sanity check on that is a var_dump($attachments);die; which dumps:

object(FormsInput)#225 (18) { ["_node":protected]=> NULL ["_id":protected]=> array(1) { ["id"]=> string(5) "32171" } ["_data":protected]=> array(6) { ["id"]=> string(5) "32171" ["form_uid"]=> string(32) "aef79b2d47722ca154788cc9c8f8de2b" ["data_array"]=> string(770) "a:20:{s:9:...

How to I extract the file and push to a directory. I tried this:

$file = $attachments->getValue('file');
  if($file) {
  $filename = sha1($file->getOriginalName()).$file->getExtension($file->getOriginalExtension());
  $file->save(sfConfig::get('sf_upload_dir').'/'.$formTitle.'/'.$usrDir.''.$filename);
}

but it throws this error:

Unknown record property / related component "value" on "FormsInput"

How can I get the uploaded into a sf dir?

UPDATE

Tried to use the sfValidatorFile class, but I cannot figure out how to pass it the parameters from the $attachment object:

$file = new sfValidatorFile();
if($file) {
$filename = sha1($file->getOriginalName()).$file->getExtension($file->getOriginalExtension());
$file->save(sfConfig::get('sf_upload_dir').'/uploadedDir');
}

throws this:

Fatal error: Call to undefined method sfValidatorFile::save() 

Upvotes: 1

Views: 565

Answers (1)

sinhix
sinhix

Reputation: 863

What you intended to do with the $file->save requires an sfValidatedFile object. If FormsInput is an instance of sfFormObject, you can configure your form with 4 sfValidatorFile with the path you wanted in option. It will do the job you need.

Upvotes: 3

Related Questions