1

I am using CakePHP 2.1 and trying to upload files into server.

I can successfully move the files to specified directory with manually naming a file, but its not taking the name from the file type from view: register.

Database table: users

Id Auto_Increment
username
file_name

Controller : UsersController.php

public function register(){
    if ($this->request->is('post')){
        //   $this->data['User']['resume_file']['tmp_name'] = 'test.php';
        //  The above statement is working good

        if ($this->User->save($this->request->data)){
            $this->Session->setFlash('Message : ' . $this->data['User']['resume_file']['tmp_name']);
            //debug($this->data['User']['resume_file']['tmp_name']);

            // This is giving an error, while inserting
        }
    }
}

View : users/register.php

<?php
    echo $this->Form->create('User', array('type'=>'file'));
    echo $this->Form->input('username');
    echo $this->Form->input('file_name', array('type'=>'file', 'label'=>'Upload document') );
    echo $this->Form->end('Register');
?>

Error : Database error

INSERT INTO `sample_db`.`users` (`file_name`, `created`) VALUES (Array, '2012-06-14 08:10:10')

What i seen is that, while insert into table, Array is inserted thats and error Array to string conversion error in CakePHP now how can i get the value of that input type : file_name in CakePHP

Or else if we can modify the data before saving with the below request statement..

$this->request->data

I tried to change that, with below statement

$this->data['User']['resume_file']['tmp_name'] = 'test.php';

but couldn't its returning same error, any idea...

1 Answer 1

3

Try this:

public function register(){
    if ($this->request->is('post')){
        $filename = $this->request->data['User']['file_name']['tmp_name'];
        $this->data['User']['file_name'] = $filename;

        if ($this->User->save($this->request->data)){
            $this->Session->setFlash('Message : ' . $filename);
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Its still gives the same error (file_name, created) VALUES (Array, '2012-06-14 08:10:10') and Notice Indirect modification of overloaded property UsersController::$data has no effect [APP\Controller\UsersController.php, line 39]
I had edited you answer with commented statement, that is perfectly good.. but it taking the temporary value like D:\xampp\tmp\php5500.tmp how can i retrieve the original filename..
@Rafee: Instead of ['tmp_name'] use ['name']. You are welcome!
okay any idea, how to get extension of file stackoverflow.com/questions/11031567/…
it works for me with some changes. $this->request->data instead of $this->data

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.