0

I found a tutorial showing how to upload a file and store it in DB as blob. This is my code in FilesController:

$zip_file=$this->data['File']['zip'];
$fileData = fread(fopen($zip_file['tmp_name'], "r"), $zip_file['size']);

$this->data['File']['zip'] = $fileData;

$this->File->save($this->data);

getting this error upon execution:

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'
SQL Query: INSERT INTO `my_db`.`files` (`task_id`, `zip`) VALUES (5, Array) 

Obviously it's the problem because $zip is an array. How to solve it?


DEBUG:

var_dump($this->data['File']);:

array (size=2)
   'task_id' => string '5' (length=1)
   'zip' => 
       array (size=5)
          'name' => string 'Vaja1.zip' (length=9)
          'type' => string 'application/x-zip-compressed' (length=28)
          'tmp_name' => string 'C:\wamp\tmp\php50B9.tmp' (length=23)
          'error' => int 0
          'size' => int 847624
2
  • Have you var_dumped $this->data['File'] before or after $this->data['File']['zip'] = $fileData;? Because after this line it should be a string (well or FALSE), not an array. Commented Jan 15, 2013 at 10:02
  • Hm, funny stuff - dumping $fileData displays a string but dumping $this->data['File']['zip'] displays an array. Looks like line $this->data['File']['zip'] = $fileData; has no effect. Is this possible? Commented Jan 15, 2013 at 10:08

1 Answer 1

1

Alright, found an answer - in short:

I had to replace every $this->data with $this->request->data and now it works.

Sign up to request clarification or add additional context in comments.

3 Comments

CakePHP 2.0+ changed to using $this->request->data. It's something you need to watch out for when looking at Cake tutorials online as many use older Cake conventions.
Good tip! Checking for every construct deprecation can be frustrating though.
@PrimožKralj look at the upgrade shell, it will do most of those replacements for you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.