Exemplo n.º 1
0
 public function upload()
 {
     $files = $this->request->data('files');
     $config =& $this->config[$this->request->data('config')];
     $output = [];
     $config['directory'] = realpath(WWW_ROOT . $config['path']);
     foreach ($files as $file) {
         if (!in_array(strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)), $config['allowed'])) {
             continue;
         }
         $name = uniqid('', true) . '.' . strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
         $config['destination'] = $config['directory'] . DS . $name;
         $config['url'] = str_replace('//', '/', "{$config['path']}/{$name}");
         $config['rel_table'] = $config['model'];
         $image = new Image($file['tmp_name']);
         $image->maxWidth(1200);
         $name = uniqid('', true) . '.' . pathinfo($file['name'], PATHINFO_EXTENSION);
         $image->save($config['destination']);
         $entity = $this->Galleries->newEntity($config);
         if ($result = $this->Galleries->save($entity)) {
             $output[] = ['id' => $result->id, 'url' => $entity->url];
         }
     }
     echo json_encode($output);
 }