图片将不会与cakephp-upload行为一起上传

问题描述:

我不想在我的cakephp 3.x应用程序中建立一个简单的上传功能。我使用了cakephp-upload behavior。在这一刻,我可以将所有需要的数据保存到数据库中,但图像不会被上传。我在这里检查了一些相关的文章在stackoverflow但是主要为cakephp 2.x和不是为版本3.x.图片将不会与cakephp-upload行为一起上传

的看法是这样的:

<?php echo $this->Form->create($form, ['type' => 'file', 'id' => 'form', 'class' => 'form-horizontal', 'url' => '/storages/form/content/' . $formId]); ?> 

// some other input fields 

<?php echo $this->Form->input('photo', ['type' => 'file']); ?> 
<?php echo $this->form->end(); ?> 

的功能,节省相关的东西到数据库中所有的照片看起来像这样:

// the $array consists of this data 
    [ 
     'data||1' => 'data', 
     'photo' => [ 
      'tmp_name' => '/Applications/MAMP/tmp/php/phpHbVBQz', 
      'error' => (int) 0, 
      'name' => 'Screen Shot 2017-08-22 at 14.26.17.png', 
      'type' => 'image/png', 
      'size' => (int) 163117 
     ] 
] 

private function addContent($array) { 
     $form = $this->getHighestFormId(); 

     foreach ($array as $field => $value) { 
      if ($value != null) { 
       // create new entity 
       $containerContent = $this->StoragecontainerContent->newEntity(); 

       // explode field name and container_block_element_id 
       $explodedField = explode("||", $field); // [0] field, [1] container_block_element_id 

       // remove the automatic generated _ character from the string and replace it by a whitespace 
       // $replacedName = str_replace("_", " ", $explodedField[0]); 

       // create array 
       $data = [ 
        'user_id' => $this->Auth->user('id'), 
        'form_id' => $form->maxFormId + 1, 
        'storagecontainer_block_element_id' => $explodedField[1], 
        'identifier' => $explodedField[0], 
        'content' => $value 
       ]; 

       // patch data 
       $containerContent = $this->StoragecontainerContent->patchEntity($containerContent, $data); 

       // safe value 
       $this->StoragecontainerContent->save($containerContent); 
      } 
     } 
    } 

最后,我的表类长相像这样:

class StoragecontainerContentTable extends Table { 

    /** 
    * Initialize method 
    * 
    * @param array $config The configuration for the Table. 
    * @return void 
    */ 
    public function initialize(array $config) { 
     parent::initialize($config); 

     $this->setTable('storagecontainer_content'); 
     $this->setPrimaryKey('id'); 

     $this->addBehavior('Josegonzalez/Upload.Upload', [ 
      'photo' => [ 
       'fields' => [ 
        'dir' => 'photo_dir', 
        'size' => 'photo_size', 
        'type' => 'photo_type' 
       ], 
       'nameCallback' => function ($data, $settings) { 
        return strtolower($data['name']); 
       }, 
       'transformer' => function ($table, $entity, $data, $field, $settings) { 
        $extension = pathinfo($data['name'], PATHINFO_EXTENSION); 

        // Store the thumbnail in a temporary file 
        $tmp = tempnam(sys_get_temp_dir(), 'upload') . '.' . $extension; 

        // Use the Imagine library to DO THE THING 
        $size = new \Imagine\Image\Box(40, 40); 
        $mode = \Imagine\Image\ImageInterface::THUMBNAIL_INSET; 
        $imagine = new \Imagine\Gd\Imagine(); 

        // Save that modified file to our temp file 
        $imagine->open($data['tmp_name']) 
         ->thumbnail($size, $mode) 
         ->save($tmp); 

        // Now return the original *and* the thumbnail 
        return [ 
         $data['tmp_name'] => $data['name'], 
         $tmp => 'thumbnail-' . $data['name'], 
        ]; 
       }, 
       'deleteCallback' => function ($path, $entity, $field, $settings) { 
        // When deleting the entity, both the original and the thumbnail will be removed 
        // when keepFilesOnDelete is set to false 
        return [ 
         $path . $entity->{$field}, 
         $path . 'thumbnail-' . $entity->{$field} 
        ]; 
       }, 
       'keepFilesOnDelete' => false 
      ] 
     ]); 
    } 
} 

您不会构建自定义数据集并保存该数据集,实际上在保存过程中没有什么特别的要求,这就是为什么它不在文档中。

您只需在表单中使用已配置的名称,然后使用请求数据修改实体而不进行任何修改,然后保存该实体并上传行为将完成剩余的操作,即如果$array是原始请求数据,则将其直接传递给patchEntity()

+0

但是有可能让行为将数据保存在其他表中吗? Beqouse在它保存在用户表中的例子中,这不是我想要的。 – CodeWhisperer

+0

@CodeWhisperer当然这是可能的,数据将被保存到行为所附的任何表格中。 – ndm

+0

我改变了我的问题。现在,行为被耦合到相关的表,但我怎样才能读取照片元素,并将其插入到另一个表中? – CodeWhisperer