php - Images won't be uploaded with the cakephp-upload behavoir -
i wan't build simple upload functionality inside cakephp 3.x application. have used cakephp-upload behavior. on moment can save needed data database, image won't uploaded. i've checked related articles here on stackoverflow cakephp 2.x , not version 3.x.
the view looks this:
<?php echo $this->form->create($form, ['type' => 'file', 'id' => 'form', 'class' => 'form-horizontal', 'url' => '/storages/form/content/' . $formid]); ?> // other input fields <?php echo $this->form->input('photo', ['type' => 'file']); ?> <?php echo $this->form->end(); ?>
the function save photo related stuff database looks this:
// $array consists of data [ 'data||1' => 'data', 'photo' => [ 'tmp_name' => '/applications/mamp/tmp/php/phphbvbqz', 'error' => (int) 0, 'name' => 'screen shot 2017-08-22 @ 14.26.17.png', 'type' => 'image/png', 'size' => (int) 163117 ] ] private function addcontent($array) { $form = $this->gethighestformid(); foreach ($array $field => $value) { if ($value != null) { // create new entity $containercontent = $this->storagecontainercontent->newentity(); // explode field name , container_block_element_id $explodedfield = explode("||", $field); // [0] field, [1] container_block_element_id // remove automatic generated _ character string , replace 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); } } }
finally, table class looks this:
class storagecontainercontenttable extends table { /** * initialize method * * @param array $config configuration 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 thumbnail in temporary file $tmp = tempnam(sys_get_temp_dir(), 'upload') . '.' . $extension; // use imagine library thing $size = new \imagine\image\box(40, 40); $mode = \imagine\image\imageinterface::thumbnail_inset; $imagine = new \imagine\gd\imagine(); // save modified file our temp file $imagine->open($data['tmp_name']) ->thumbnail($size, $mode) ->save($tmp); // return original *and* thumbnail return [ $data['tmp_name'] => $data['name'], $tmp => 'thumbnail-' . $data['name'], ]; }, 'deletecallback' => function ($path, $entity, $field, $settings) { // when deleting entity, both original , thumbnail removed // when keepfilesondelete set false return [ $path . $entity->{$field}, $path . 'thumbnail-' . $entity->{$field} ]; }, 'keepfilesondelete' => false ] ]); } }
you not build custom dataset , save that, there's nothing special need in saving process, that's why it's not in docs.
all need use configured name in form you're doing it, , patch entity request data without modifications, save , upload behavior rest, ie if $array
raw request data, pass directly patchentity()
.
Comments
Post a Comment