First of all remember to import the file and folder Joomla helpers:
jimport('joomla.filesystem.file'); jimport('joomla.filesystem.folder');
Function has parameters to set if we want to create the destination folder or overwrite existing files.
Returns 0 if found any errors, 1 if creation was succesful or null if the destination file already exists.
/** * Move a file with creating destination folder && overwrite options * @author Roberto Segura - Digital Disseny, S.L. * @version 11/07/2012 * * @param string $srcFile * @param string $destFile * @param boolean $createDestFolder * @param boolean $overwrite * * @return 0->error | 1->ok | null->already exists */ static public function moveFile($srcFile, $destFile, $createDestFolder = true, $overwrite = false) { if (JFile::exists($srcFile )) { if (!JFile::exists($destFile) || $overwrite) { // do we have to create the destination folder ? $destFolder = str_replace(JFile::getName($destFile), '',$destFile); if (!JFolder::exists($destFolder)) { if (!$createDestFolder || !JFolder::create($destFolder)) { return 0; } } // move the file if (JFile::move($srcFile, $destFile)) { return 1; } // destination already exists and we don't want to overwrite } else { return null; } } return 0; }