如何保存输入文件中图像的名称


How to save the name of the image from an input file

我的数据库中有一个名为image_cover的字段类型string。我的表单上有一个类型为file的字段:

<?php
  echo $this->Form->create($event, ['type' => 'file'])
    echo $this->Form->file('image_cover')
    echo $this->Form->submit('Send')
  echo $this->Form->end()
?>

控制器$this->request->data('image_cover')上有一个数组,包含文件名、大小等。

我只需要将image_cover的值更改为文件名。

我尝试在beforeSave上执行此操作,但无法使用

有几种方法可以做到这一点,但最容易理解的是使用一个不属于表模式的输入字段。例如,您可以使用image_file:而不是image_cover

 echo $this->Form->file('image_file');

确保在实体的$_accessible字段列表中设置了image_file。您将能够访问beforeSave中的此字段,以便存储它。

有一个插件可以为您做到这一点:https://github.com/davidyell/CakePHP3-Proffer