OCA\Richdocuments\Helper::getNewFileName PHP Method

getNewFileName() public static method

public static getNewFileName ( $view, $path, $prepend = ' ' )
    public static function getNewFileName($view, $path, $prepend = ' ')
    {
        $fileNum = 1;
        while ($view->file_exists($path)) {
            $fileNum += 1;
            $path = preg_replace('/(\\.|' . $prepend . '\\(\\d+\\)\\.)([^.]*)$/', $prepend . '(' . $fileNum . ').$2', $path);
        }
        return $path;
    }

Usage Example

 /**
  * @NoAdminRequired
  */
 public function create()
 {
     $mimetype = $this->request->post['mimetype'];
     $view = new View('/' . $this->uid . '/files');
     $dir = $this->settings->getUserValue($this->uid, $this->appName, 'save_path', '/');
     if (!$view->is_dir($dir)) {
         $dir = '/';
     }
     $basename = $this->l10n->t('New Document.odt');
     switch ($mimetype) {
         case 'application/vnd.oasis.opendocument.spreadsheet':
             $basename = $this->l10n->t('New Spreadsheet.ods');
             break;
         case 'application/vnd.oasis.opendocument.presentation':
             $basename = $this->l10n->t('New Presentation.odp');
             break;
         default:
             // to be safe
             $mimetype = 'application/vnd.oasis.opendocument.text';
             break;
     }
     $path = Helper::getNewFileName($view, $dir . '/' . $basename);
     $content = '';
     if (class_exists('\\OC\\Files\\Type\\TemplateManager')) {
         $manager = \OC_Helper::getFileTemplateManager();
         $content = $manager->getTemplate($mimetype);
     }
     if (!$content) {
         $content = file_get_contents(dirname(__DIR__) . self::ODT_TEMPLATE_PATH);
     }
     if ($content && $view->file_put_contents($path, $content)) {
         $info = $view->getFileInfo($path);
         $response = array('status' => 'success', 'fileid' => $info['fileid']);
     } else {
         $response = array('status' => 'error', 'message' => (string) $this->l10n->t('Can\'t create document'));
     }
     return $response;
 }
All Usage Examples Of OCA\Richdocuments\Helper::getNewFileName