3

I am working on a CakePHP project. Here, I have to upload a file and then move that file to a specific folder. Uploading is working well, but I can't move it to the folder I want. Is there any function or something in CakePHP to move the uploaded file ?

Currently I am using PHP's core function move_uploaded_file(). This function requires absolute path for both source & destination, not the http://localhost/...., I tried it, but it didn't work. So, do you know how do I get the Base Directory/Absolute Path of my application ? This would be like : C://wamp/www/project_folder/.......

user2387319
  • 123
  • 1
  • 3
  • 12

3 Answers3

3

Add function in Controller

public function add() {
    $this->Model->create();
    if ($this->request->is('post')) {
        if(!empty($this->data['Model']['image']['name']))
        {
        $file=$this->data['Model']['image'];
        $ary_ext=array('jpg','jpeg','gif','png');
        $ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
            if(in_array($ext, $ary_ext))
            {
                move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img/uploads/posts/' . $file['name']);
                $this->request->data['Model']['image'] = $file['name'];
            }   
        }
        if ($this->Model->save($this->request->data)) 
        {
            $this->Session->setFlash('Your record has been saved.');
            $this->redirect(array('action' => 'index'));
        }
        else 
        {
            $this->Session->setFlash('Unable to add your record.');
        }
    }
}


File control in add.ctp file

echo $this->Form->input('image',array('type'=>'file'));


you can define constant in index.php file like below
/**
 *  Get Cake's root directory
 */
define('APP_DIR', 'app');
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(__FILE__));
define('WEBROOT_DIR', 'webroot');
define('WWW_ROOT', ROOT . DS . APP_DIR . DS . WEBROOT_DIR . DS);
Ketan
  • 428
  • 3
  • 15
2

The constants that Cake generates (or hands you over...) are documented in the Global Constants and Functions.

Using Cake's global APP seems the way to go (unless you don't want to move the files in the APP/ folder).

JorickL
  • 165
  • 10
  • Yeah, that constant seems to be useful for me. But, how to use the constants ? $this->App ?@JorickL – user2387319 Aug 06 '13 at 08:25
  • 1
    No, just by putting APP somewhere in your PHP code. It'll convert automaticaly. $var = APP . DS . 'your_folder'; – JorickL Aug 06 '13 at 12:05
  • And, does "DS" work for all kind of servers ? I mean, in my localhost, I need "/" and in the server "\". So, does it give the right character on all platforms, or, I have to configure it for this ?@Jorickl – user2387319 Aug 06 '13 at 18:58
  • 1
    As the books says: > Short for PHP’s DIRECTORY_SEPARATOR, which is / on Linux and \ on windows. Why don't you just give it a try and echo it somewhere in one of your views? If you `
    ` them, you'll find out what URL you just created... ;)
    – JorickL Aug 07 '13 at 07:51
  • Yeah, I am now using it.@Jorickl – user2387319 Aug 07 '13 at 12:40
1

You could use php function getcwd() to get the base dir of your app.

Phillaf
  • 142
  • 1
  • 9