【问题标题】:Image Upload in Cakephp在 Cakephp 中上传图片
【发布时间】:2013-05-31 02:06:43
【问题描述】:

我想从用户表单上传图片。但是我的代码不起作用。请帮帮我

我的查看代码在这里:

echo $this->Form->create('User',array('enctype'=>'multipart/form-data'));
echo $this->Form->input('cat_image',array('type'=>'file'));
echo $this->Form->end('Submit');

我的控制器代码在这里:

move_uploaded_file($_FILES['tmp_name'], WWW_ROOT . 'img/uploads/' . $_FILES['cat_image']);

【问题讨论】:

    标签: image function cakephp upload


    【解决方案1】:

    您必须在表单中添加“类型”标签:

    <?php echo $this->Form->create('User', array('type' => 'file'));?>
    

    然后在Controller中做这样的事情:

    if (is_uploaded_file($data['User']['image']['tmp_name']))
    {
        move_uploaded_file(
            $this->request->data['User']['image']['tmp_name'],
            '/path/to/your/image/dir/' . $this->request->data['User']['image']['name']
        );
    
        // store the filename in the array to be saved to the db
        $this->request->data['User']['image'] = $this->request->data['User']['image']['name'];
    }
    

    并像这样查看以在网页中显示图像:

    <?php echo $this->Html->image('/path/to/your/image/dir/' . $listShExPainImage['User']['image']); ?>
    

    如果我能帮助你更多,请告诉我。

    【讨论】:

      【解决方案2】:

      改变

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

      echo $this->Form->create('User',array('type' => 'file'));
      

      输出

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多