file upload in cakephp 2.3

Hovo picture Hovo · Apr 28, 2013 · Viewed 48.4k times · Source

I'm new in cakephp and i'm trying to create a simple file upload with cakephp 2.3 here is my controller

public function add() {
    if ($this->request->is('post')) {
        $this->Post->create();
           $filename = WWW_ROOT. DS . 'documents'.DS.$this->data['posts']['doc_file']['name']; 
           move_uploaded_file($this->data['posts']['doc_file']['tmp_name'],$filename);  


        if ($this->Post->save($this->request->data)) {
            $this->Session->setFlash('Your post has been saved.');
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash('Unable to add your post.');
        }
     }
 }

and my add.ctp

echo $this->Form->create('Post');
echo $this->Form->input('firstname');
echo $this->Form->input('lastname');
echo $this->Form->input('keywords');
echo $this->Form->create('Post', array( 'type' => 'file'));
echo $this->Form->input('doc_file',array( 'type' => 'file'));
echo $this->Form->end('Submit')

it saves firstname, lastname, keywords, and the name of the file in DB , but the file which i want to save in app/webroot/documents is not saving , can anyone help ? Thanks

Update

thaJeztah i did as u said but it gives some errors here is controller if i'm not wrong

public function add() {
     if ($this->request->is('post')) {
         $this->Post->create();
            $filename = WWW_ROOT. DS . 'documents'.DS.$this->request->data['Post']['doc_file']['name']; 
           move_uploaded_file($this->data['posts']['doc_file']['tmp_name'],$filename);



         if ($this->Post->save($this->request->data)) {
             $this->Session->setFlash('Your post has been saved.');
             $this->redirect(array('action' => 'index'));
         } else {
            $this->Session->setFlash('Unable to add your post.');
         }
     }

 }

and my add.ctp

 echo $this->Form->create('Post', array( 'type' => 'file'));
 echo $this->Form->input('firstname'); echo $this->Form->input('lastname');
 echo $this->Form->input('keywords');
 echo $this->Form->input('doc_file',array( 'type' => 'file'));
 echo $this->Form->end('Submit') 

and the errors are

Notice (8): Array to string conversion [CORE\Cake\Model\Datasource\DboSource.php, line 1005]

Database Error Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'

SQL Query: INSERT INTO first.posts (firstname, lastname, keywords, doc_file) VALUES ('dfg', 'cbhcfb', 'dfdbd', Array)

and Victor i did your version too , it doesnt work too .

Answer

thaJeztah picture thaJeztah · Apr 29, 2013

You seem to be using the wrong 'key' to access the posted data;

$this->data['posts'][....

Should match the 'alias' of you Model; singular and a captial first letter

$this->data['Post'][....

Also, $this->data is a wrapper for $this->request->data for backwards compatibility, so it's better to use this;

$this->request->data['Post'][...

To check the content of the posted data and understand how it's structured, you may debug it using this;

debug($this->request);

Just be sure to enable debugging, by setting debug to 1 or 2 inside app/Config/core.php

Update; duplicate Form tags!

I just noticed you're also creating multiple (nested) forms in your code;

echo $this->Form->input('keywords');

// This creates ANOTHER form INSIDE the previous one!
echo $this->Form->create('Post', array( 'type' => 'file'));

echo $this->Form->input('doc_file',array( 'type' => 'file'));

Nesting forms will never work, remove that line and add the 'type => file' to the first Form->create()

Using only the file name for the database

The "Array to string conversion" problem is cause by the fact that you're trying to directly use the data of 'doc_file' for your database. Because this is a file-upload field, 'doc_file' will contain an Array of data ('name', 'tmp_name' etc.).

For your database, you only need the 'name' of that array so you need to modify the data before saving it to your database.

For example this way;

// Initialize filename-variable
$filename = null;

if (
    !empty($this->request->data['Post']['doc_file']['tmp_name'])
    && is_uploaded_file($this->request->data['Post']['doc_file']['tmp_name'])
) {
    // Strip path information
    $filename = basename($this->request->data['Post']['doc_file']['name']); 
    move_uploaded_file(
        $this->data['Post']['doc_file']['tmp_name'],
        WWW_ROOT . DS . 'documents' . DS . $filename
    );
}

// Set the file-name only to save in the database
$this->data['Post']['doc_file'] = $filename;