Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP copy file to new directory

The cakephp documentation is a bit poor according the File api and I would like to use it to copy one file to another directory and if the directory does not exist to create it.

Thanks!

like image 365
Constantin.FF Avatar asked Jan 17 '23 08:01

Constantin.FF


1 Answers

You can do something like this:

$file = new File('your_file.ext');

if ($file->exists()) {
    $dir = new Folder('folder_inside_webroot', true);
    $file->copy($dir->path . DS . $file->name);
}

API about these classes:

  • Folder;
  • File;

I hope it helps.

like image 163
Paulo Rodrigues Avatar answered Jan 30 '23 13:01

Paulo Rodrigues