1

如此简单的事情怎么会如此困难,这太疯狂了。

请致电所有 cakephp 2.1 大师。我有一个表单,它有一个名称和文本图像 (var) 字段。

我想简单地发布一张图片,没有调整大小,没有缩略图,没有什么特别的,只是一个简单的 mvc 示例,发布一张图片并在 view.ctp 中查看它。

而已。我已经浏览了 cakephp.org 上的 /en/2.1 书和 api,但我似乎无法让 mvc 工作,因为看到它上传到文件夹并在 ctp 中查看它。

我的控制器

public function add() {
    if ($this->request->is('post')) {
        $this->ListShExPainImage->create();
        if ($this->ListShExPainImage->save($this->request->data)) {
            $this->Session->setFlash(__('The list sh ex pain image has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The list sh ex pain image could not be saved. Please, try again.'));
        }
    }
}

public function view($id = null) {
    $this->ListShExPainImage->id = $id;
    if (!$this->ListShExPainImage->exists()) {
        throw new NotFoundException(__('Invalid list sh ex pain image'));
    }
    $this->set('listShExPainImage', $this->ListShExPainImage->read(null, $id));
}

我的 add.ctp

<?php echo $this->Form->create('ListShExPainImage');?>
<fieldset>
    <legend><?php echo __('Add List Sh Ex Pain Image'); ?></legend>
<?php
    echo $this->Form->input('name');
    echo $this->Form->input('image', array('type' => 'file'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>

我的观点.ctp

<h2><?php  echo __('List Sh Ex Pain Image');?></h2>
<dl>
    <dt><?php echo __('Id'); ?></dt>
    <dd>
        <?php echo h($listShExPainImage['ListShExPainImage']['id']); ?>
        &nbsp;
    </dd>
    <dt><?php echo __('Name'); ?></dt>
    <dd>
        <?php echo h($listShExPainImage['ListShExPainImage']['name']); ?>
        &nbsp;
    </dd>
    <dt><?php echo __('Image'); ?></dt>
    <dd>
        <?php echo h($listShExPainImage['ListShExPainImage']['image']); ?>
        &nbsp;
    </dd>
</dl>

非常感谢您的帮助,在此先感谢您。

4

2 回答 2

1

在您的控制器中,您没有对文件上传做任何事情。尽管 Cake 有时很神奇,但自动猜测你想用那个上传做什么并不是其中之一。

书中所述,文件上传字段返回一个包含多个键的数据数组。您应该获取name密钥并将其保存为image字段(因为这就是您似乎在调用的内容)。例如:

$filename = $this->request->data['ListShExPainImage']['image']['name'];

// Move the image
move_uploaded_file(
    $this->request->data['ListShExPainImage']['image']['tmp_name'],
    '/path/to/your/image/dir/' . basename($filename)'
);

// Set the filename in the database
$this->request->data['ListShExPainImage']['image'] = $filename;

此外,您需要为您的表单设置一个所谓的multipart/form-dataenctype,以便能够通过将文件类型添加到表单创建选项来解析图像上传。

另外,不要忘记将文件从临时文件夹移动到最终位置。

于 2012-05-18T22:56:02.430 回答
0

您还需要在表单中添加“类型”:

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

然后在控制器中做这样的事情:

if (is_uploaded_file($data['ListShExPainImage']['image']['tmp_name']))
{
    move_uploaded_file(
        $this->request->data['ListShExPainImage']['image']['tmp_name'],
        '/path/to/your/image/dir/' . $this->request->data['ListShExPainImage']['image']['name']
    );

    // store the filename in the array to be saved to the db
    $this->request->data['ListShExPainImage']['image'] = $this->request->data['ListShExPainImage']['image']['name'];
}

然后执行您的创建和保存程序。

在您的视图中,您会执行以下操作来显示图像:

<?php echo $this->Html->image('/path/to/your/image/dir/' . $listShExPainImage['ListShExPainImage']['image']); ?>
于 2012-05-18T22:59:56.993 回答