如何上传图像在cakephp2.5 +和存储在webroot文件夹 [英] how to upload image in cakephp2.5+ and store it in webroot folder

查看:209
本文介绍了如何上传图像在cakephp2.5 +和存储在webroot文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器

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

我的查看文件

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

所以告诉我如何存储图像在webroot文件夹,只是保存名称在数据库字段varbigimg字段。我也想在页面上显示它。所以请解决我的问题感谢

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

我是cakephp感谢的新的

I am new in cakephp thanks

推荐答案

p>使用以下

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

允许处理图片/视频数据。

Allows image/video data to be processed.

内部控制器

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;

//这里你需要为thum指定相同

//HERE you need to specify same for thum

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

////这里需要为thumbfield指定

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

// 更新

//在这里明确指定代码中的输入类型。

//Specify input type in your code explicitely here.

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

这篇关于如何上传图像在cakephp2.5 +和存储在webroot文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆