【问题标题】:how to upload image in cakephp2.5+ and store it in webroot folder [closed]如何在 cakephp2.5+ 中上传图像并将其存储在 webroot 文件夹中[关闭]
【发布时间】:2014-12-03 04:44:51
【问题描述】:

我的控制器

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

我已经检查了模型,因为图像名称存储在数据库中,但我想使用该图像。我想将该图像保存在任何文件夹中,所以请帮助我。我正在使用新版本谢谢

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

我是 cakephp 新手,谢谢

【问题讨论】:

  • 这个问题读起来好像你想让别人为你写代码。尝试用一个关于您遇到的问题的具体问题以及错误的详细信息或您不理解的项目等来构建它。你都尝试了些什么?什么有效,什么无效……等等。网上有很多关于这个主题的资源,这不是一般“操作方法”的地方。
  • 我在这个话题上尝试了太多,但没有得到任何解决方案,所以我把它贴在这里

标签: cakephp image-uploading cakephp-2.3


【解决方案1】:

使用以下

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

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

内部控制器

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 指定相同的值

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

////这里你需要为拇指域指定相同

        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->input('vartitle',array('type'=>'text','class' => 'form-control','label' => 'Tour Title'));

【讨论】:

  • 感谢您的回复。但是当我粘贴这段代码时,我面临 } 错误语法错误,意外 '{' if(is_uploaded_file($this->request->data['Tour']['varbigimg']['tmp_name']) { 所以这个括号错误,我正在尝试,但无法。得到它。感谢您的建议。请解决 1 最后感谢
  • @newsurt,现在检查,一个 ( 丢失了
  • 再次感谢我得到 sql 错误 Column not found: 1054 Unknown column 'Array' in 'field list' 数组到字符串的转换 [CORE/Cake/Model/Datasource/DboSource.php, line 1009]
  • $this->request->data['Tour']['varbigimg'] = $fileNameFull;之后,使用'print_r($this->request->data['Tour']['varbigimg']);`,你得到什么价值,更新答案。跨度>
  • 我的图像存储在我的文件夹中,但我的数据没有保存在数据库中,它说数组到字符串的转换。 SQLSTATE [42S22]:未找到列:1054“字段列表”中的未知列“数组”“34”,“2014-09-17 17:16:36.png”的屏幕截图,数组)我没有提到的一件事是我通过 2 张图片。以这种形式
【解决方案2】:

伙计,我知道你需要让它变得简单。 首先,您需要访问控制器中的文件,使用 $this->data['Tour']['varbigimg']; 好的,这是你的文件。 好的,现在您需要将此文件发送到 webroot,您可以使用 cakephp 的 File Class http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html 您可以使用 WWW_ROOT 约束将文件移动和复制到 webroot,如果您需要发送到 webroot img 使用 IMAGES 常量,这个常量会为您提供到 webroot 文件夹的正确路径。 使用文件夹/文件和 cakephp 常量很容易满足您的需求。 如果您需要有关此的更多信息,请问我,我为您创建一个真实的示例。 天呐

【讨论】:

    【解决方案3】:

    您需要将上传的文件移动到文件夹中。获取$this->request->data 的文件信息并移动到webroot 中的文件夹。

    有很多插件可以做到这一点。点赞:Upload Plugin 2.0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      • 1970-01-01
      相关资源
      最近更新 更多