2

I do not know how to store an image in db and display it?

What i want to do is to store image path in db and store the image in /img/ folder and then display it to the user.

In my registration form i am asking user to upload image like this

<?php echo $this->Form->input('image', array('class'=>'forminput', 'label' => 'Upload Image:', 'type' => 'file'));  ?>

and in database type of image field is longblob.

Displaying it to the user like this...

<?php echo $this->html->image('Auth.User.image');?>

2 Answers 2

2

You store the image on the server and in the DB you save only the name of the image.

Usualy the images are stored in img folder in webroot. When you will display the image you will have something like this:

<?php echo $this->Html->image('img' . DS . CakeSession::read('Auth.User.image')); ?>
  • where image is the name of the image you are uploading saved to the image field in the Users table.

Inside the controller you should upload the file as you do normaly in PHP.

$file_name = $_POST['User']['name'];
$tmp_file = $_POST['User']['tmp_name'];
if(is_uploaded_file($tmp_file)){ 
    if(move_uploaded_file($tmp_file){
        ... 
        $this->data['User']['image'] = $file_name; 
        $this->User->save($this->data);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I dont know but what exactly should i do and how? I am new to CakePHP so do not have any idea.
You should upload the file as you do normal in PHP. if(is_uploaded_file($tmp_file)){ if(move_uploaded_file()... $this->data['User']['$file_name']; $this->User->save($this->data);
i edited my answer. is still not clear enough? Do you know PHP?
0
paste it in your controller and change the field and model names accroding to 

public function add() {

    $this->set('status_tbl', $this->Player->Status->find('list'));


        if ($this->request->is('post')) {
             $this->Player->create();
             if(!empty($this->request->data)){

                if(!empty($this->request->data['Player']['image']['name'])){

                $file = $this->request->data['Player']['image']; 

                $ext = substr(strtolower(strrchr($file['name'], '.')), 1); 

                $arr_ext = array('jpg', 'jpeg', 'gif'); 


        if(in_array($ext, $arr_ext)){
        $this->request->data['Player']['image'] = $file['name'];
        move_uploaded_file($file['tmp_name'],'uploads/file'.$file['name']);



        $this->Player->save($this->request->data);
        }

                }


        if($this->Player->save($this->request->data)){
        return $this->redirect(array('controller'=>'Players',"action"=>'index'));

                }
            }

        }

}

Comments

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.