Add image file using cakephp and mongodb

316 Views Asked by At

I'm new user on mongodb, I'm working with cakephp. I'm trying to test the cakephp's plugin from ichikaway. This plugin allow cakephp working with a mongodb (NoSql database). So, I'm wondering if someone have already tried to do an image upload with ?

2

There are 2 best solutions below

0
On

I try this and it works !

     <?php

      class ProductsController extends AppController {
       public $name = 'Products';


       public function add(){
      if ($this->request->is('post')){
        debug($this->request->data);
        $dir = IMAGES.date('Y');
        if (!file_exists($dir))
            mkdir($dir);
        $dir .= DS.date('m');
        if (!file_exists($dir))
            mkdir($dir);
        $f = explode('.', $this->request->data['Product']['file']['name']);
        $ext = '.'.end($f);
        $filename = Inflector::slug(implode('.',array_slice($f,0,-1)),'-');
        debug($ext);
        debug($filename);

        $data = array(
            'name' => $this->request->data['Product']['name'],
            'url' => date('Y').'/'.date('m').'/'.$filename.$ext
            );
        //debug($data);
        if ($this->Product->save($data)){
            move_uploaded_file($this->request->data['Product']['file']['tmp_name'], $dir.DS.$filename.$ext);
            $id = $this->Product->getInsertId();
            debug($id);
        }
    }
}
0
On

The cakephp-mongodb library does not seem to provide an API for interacting with GridFS. Your best bet is likely to use the MongodbSource::getMongoDb() method to obtain the MongoDB instance and then access the MongoGridFS classes directly. The PHP documentation includes several examples for storing files (including uploaded files) to GridFS.