Normally when saving a node with the content type form, uploading a file will automatically rename it if the file name is already in the database.
I am creating nodes programatically. Everything works fine, except if the file name is in the database it fails as this is, of course, a unique field.
What function does Drupal use to check if the name already exists and chanages it accordingly? I could write one, but as drupal already seems to handle this perfectly, I dont see a reason to reinvent the wheel. Here is my code for the file saves.
$file = new stdClass();
$file->filename = basename($local_path);
$file->filepath = $local_path;
$file->uri = $local_path;
$file->filemime = $mime;
$file->filesize = filesize($local_path);
$file->uid = $uid;
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
$images[] = array(
'fid' => $file->fid,
'alt' => $page_data['title'],
'title' => $page_data['title']
);