在Cakephp 3.6中如何获取图像的大小和类型以通过表单发送?

问题描述 投票:0回答:1

如何获取图像的大小和类型以通过表单发送?

那么,我想在图像表中保存通过表单上传的图像的大小和类型。使用Ajax,我可以恢复这些数据,但是要将它们传递给PHP,我只能在发送表单时,也可以直接使用PHP:

if ($this->request->is('post')) {
          $isData = $this->request->getdata();
          $imagene->imagen = $isData['image_path'];
          $imagene->tipo = $isData['type']
          $imagene->tamano = $isData['size'];
          ...

但是我想在发送表单之前这样做,这是在数据库中完成插入的时候。

形成:

<?= $this->Form->create($imagene, ['novalidate', 'id' => 'addimageform', 'class' => 'form addimageform']); ?>    
<?= $this->Form->control('imagen', ['type' => 'file', 'class' => 'imagen-addimage']); ?>
<?= $this->Form->hidden('tipo', ['value' => $tipo, 'class' => 'tipo-addimage']); ?>
<?= $this->Form->hidden('$tipo', ['value' => $size, 'class' => 'tamano-addimage']); ?>
<?= $this->Form->button('Subir imagen', ['id' => 'submit', 'class' => 'submit-addimage']); ?>
<?= $this->Form->button('Omitir', ['id' => 'omitir', 'class' => 'omitir-addimage', 'redirect' => ['controller' => 'administracion', 'action' => 'index']]); ?>
<?= $this->Form->end(); ?>

现在我看到如果我对$ isData进行调试,则字段:“imagen”不会出现:

    'tabla' => 'users',
    'id_tabla' => '22',
    'tipo' => '',
    'tamano' => ''

UPDATING

我改变了表单和控制器中的东西:

形成:

<?= $this->Form->create($imagene, ['enctype' => 'multipart/form-data', 'novalidate', 'id' => 'addimageform', 'class' => 'form addimageform']); ?>
    <?= $this->Form->control('imagen', ['type' => 'file', 'class' => 'imagen-addimage']); ?>
    <div class="centrar-submit">
      <?= $this->Form->button('Subir imagen', ['id' => 'submit', 'class' => 'submit-addimage']); ?>
      <?= $this->Form->button('Omitir', ['id' => 'omitir', 'class' => 'omitir-addimage', 'redirect' => ['controller' => 'administracion', 'action' => 'index']]); ?>
    </div>
    <?= $this->Form->end(); ?>

控制器:

public function add($table, $idTable) {
      $imagene = $this->Imagenes->newEntity();
      if ($this->request->is('post')) {
        $isData = $this->request->getdata();
        debug($this->request->getData('imagen')); // <---- Is null
        debug($isData); // <---- Is empty
        ...

为什么?我不知道。

forms image cakephp-3.0
1个回答
0
投票

您应该提到您使用的是哪种类型的表单。那是你应该使用的文件类型

echo $this->Form->create($article, ['type' => 'file']);

希望这对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.