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

时间:2017-09-12 08:19:29

标签: php cakephp cakephp-3.0

我不想在我的cakephp 3.x应用程序中构建一个简单的上传功能。我使用了cakephp-upload behavior。此时我可以将所有需要的数据保存到数据库中,但不会上传图像。我已经在stackoverflow上检查了一些相关文章,但主要是针对cakephp 2.x而不是版本3.x.

视图如下所示:

// 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
            ]
        ]);
    }
}

最后,我的表类看起来像这样:

echo '<button class="shop_tablinks tab2"  onclick='"openSpecs(event, 'Specs3')"' >';

1 个答案:

答案 0 :(得分:2)

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

您需要做的就是在表单中使用已配置的名称,并在没有任何修改的情况下使用请求数据修补实体,然后保存,上传行为将完成其余的工作,即如果$array是原始请求数据,则将其直接传递给patchEntity()