0

When uploading files you usually get an array created for the field. For example in the case where the model is Document and field is submittedfile:

$this->request->data['Document']['submittedfile'] = array(
'name' => 'conference_schedule.pdf',
'type' => 'application/pdf',
'tmp_name' => 'C:/WINDOWS/TEMP/php1EE.tmp',
'error' => 0,
'size' => 41737,
);

The array cannot be saved to the DB because the field cannot save the 'submittedfile' array to the DB 'submittedfile' column. However I only need the name section of the above array in the submittedfile field of the DB. Tried a number of ways here are two:

1 Turning the submittedfile from array to string in the Controller Method before calling the save method in Controller Methdod. (Issue could not use cakePHP validate for fileSize and uploadError as they require ['tmp_name'] to work.)

public function addDocument(){
$this->request->data['Document']['submittedfile'] = $this->request->data['Document']['submittedfile']['name'];
$this->Document->save($this->request->data);}

2 Turning the submittedfile from array to string in the beforeSave Callback Method before calling the save method in Controller Method. (Issue is that I get Error Illegal string offset 'name')

public function beforeSave($options = array()){
$this->data['Document']['submittedfile'] = $this->data['Document']['submittedfile']['name'];}

Any answers for saving uploaded file type arrays to DB?

DaGambit
  • 55
  • 1
  • 8

3 Answers3

0

If you want to save the array, you can store it as JSON or as a serialized array.

There is a discussion about the advantages of each one here

Regarding your two solutions, maybe the second one is better.

Inside the beforeSave, please debug($this->data); to see exactly what you are getting, and how you can access 'name'

How are you handling the file upload? That should be treated separately: upload the file with a random name and store that name in the database.

Community
  • 1
  • 1
cornelb
  • 6,046
  • 3
  • 19
  • 30
  • I was using the debug as you suggested found it gave the following. – DaGambit Mar 04 '14 at 15:28
  • I was using the debug as you suggested but it gave me the array structure. It showed that using `$this->data['Document']['submittedfile']['name']` should return the correct value. The problem seems to be with the assignment to `$this->data['Document']['submittedfile']` because if I echo `$this->data['Document']['submittedfile']['name']` the value is shown without the **Illegal offset error**.ted found it gave the following. – DaGambit Mar 04 '14 at 15:37
  • Sorry about the first comment clicked enter by mistake. – DaGambit Mar 04 '14 at 15:49
0

I am not sure what will you do with just the name in the db, you need to upload the file too I guess.

Use this: Uploader

skywalker
  • 826
  • 1
  • 10
  • 18
0

Problem: Multiple Save Methods in the controller that used different $this->data arrays. Tried to simplify the code posted and took out the issue from the code. The original code not shown contained two save() methods called within the adddocument() controller and one did not have the $this->data['Document']['submittedfile']['name'] passed to it. This caused the illegal string offset error:

Tip 1(recommended):

Use isset() to check that the variable exists before trying to manipulate it in the beforeSave() method:

public function beforeSave($options=array())
{
  if(isset($this->data['Document']['submittedfile']['name']))
  {$this->data['Document']['submittedfile'] = $this->data['Document']['submittedfile']['name'];
  }
   return true;
}

Tip 2:

Disable Callbacks (i.e. beforeSave, afterValidate etc.) being called by other save methods in the controller. If those saves don't require manipulation of their data beforeSave:

$this->Document->saveField('submittedfile', $yourvalue, array('callbacks'=>false))

Thanks to all those that took time to help me out @cornelb and @skywalker.

DaGambit
  • 55
  • 1
  • 8