PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, April 22, 2022

[FIXED] how to upload image in cakephp2.5+ and store it in webroot folder

 April 22, 2022     cakephp, cakephp-2.3, image-uploading     No comments   

Issue

my controller

public function add() {

    $this->helpers = array('TinyMCE.TinyMCE');
    $this->layout = 'adminpanel';
    if ($this->request->is('post')) {
        $this->Tour->create();
        if ($this->Tour->save($this->request->data)) {
            $this->Session->setFlash(__('Your possstt has been saved.'));
            return $this->redirect(array('controller'=>'admin','action' => 'tour'));
        }
        $this->Session->setFlash(__('Unable to add your schedule.'));
    }

my view file

echo $this->Form->create('Tour');
echo $this->Form->input('vartitle',array('class' => 'form-control','label' => 'Tour Title'));
// etc.................
echo $this->Form->input('varbigimg', array('type' => 'file'));

echo $this->Form->end('Save Post',array('class' => 'form-control'));

I already check model because image name are stored in database but I want to use that image. I want to save that image in any folder so help me. I am using new version thanks

So tell me how to store image in webroot folder and just save name in database field varbigimg field. I also want to display it on page too. So please solve my problem thanks

I am new in cakephp thanks


Solution

Use following

echo $this->Form->create('Tour',array('autocomplete' => 'off','enctype'=>'multipart/form-data'));

Allows image/video data to be processed.

Inside controller

if ($this->request->is('post')) {

    if(is_uploaded_file($this->request->data['Tour']['varbigimg']['tmp_name']))
    {
        $fileNameFull = $this->request->data['Tour']['varbigimg']['name'];

        $oldFile = '../../app/webroot/img/'.$fileNameFull; 

        move_uploaded_file(
          $this->request->data['Tour']['varbigimg']['tmp_name'],
          $oldFile
        );

        $this->Tour->create();
        $this->request->data['Tour']['varbigimg'] = $fileNameFull;

//HERE you need to specify same for thum

        $this->request->data['Tour']['varthumimg'] = $fileNameFull;

////HERE you need to specify same for thumbfield

        if ($this->Tour->save($this->request->data)) {
            $this->Session->setFlash(__('Your possstt has been saved.'));
            return $this->redirect(array('controller'=>'admin','action' => 'tour'));
        }

    }
}
$this->Session->setFlash(__('Unable to add your schedule.'));

//Update

//Specify input type in your code explicitely here.

echo $this->Form->input('vartitle',array('type'=>'text','class' => 'form-control','label' => 'Tour Title'));


Answered By - Pratik
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing