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

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 上执行此操作,但无法正常工作

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

而不是 image_cover
 echo $this->Form->file('image_file');

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

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