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

♀尐吖头ヾ 提交于 2019-12-25 19:57:11

问题


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


回答1:


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'));



回答2:


Man , i get to you the needs to make it simply. First you need access the file in your controller, using $this->data['Tour']['varbigimg']; ok , it's your file. Ok, now you need send this file to webroot, you can use File Class of the cakephp http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html You can move and copy files to webroot using WWW_ROOT constraint, this constant get to you the correct path to webroot folder, if u need send to webroot img use IMAGES constant. Using, Folder/File and cakephp constants it's very easy to make your needs. If you need a more information about this, plz ask me and i create a real example to you. Tnx




回答3:


You need to move your uploaded file to a folder. Get the file info of your $this->request->data and move to a folder in webroot.

There are many plugins for that. Like: Upload Plugin 2.0



来源:https://stackoverflow.com/questions/26258330/how-to-upload-image-in-cakephp2-5-and-store-it-in-webroot-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!