You are here

function webform_file_process_rename in Webform 7.4

Renames the uploaded file name using tokens.

Parameters

object $node: The webform node object.

object $submission: The webform submission object.

array $component: Component settings array for which fid is going to be processed.

int $fid: A file id to be processed.

1 call to webform_file_process_rename()
webform_file_rename in components/file.inc
Rename any files which are eligible for renaming.

File

components/file.inc, line 644
Webform module file component.

Code

function webform_file_process_rename($node, $submission, $component, $fid) {
  $file = webform_get_file($fid);
  if ($file) {

    // Get the destination uri.
    $destination_dir = $component['extra']['scheme'] . '://webform/' . drupal_strtolower(webform_replace_tokens($component['extra']['directory'], $node));
    $destination_dir = file_stream_wrapper_uri_normalize($destination_dir);

    // Get the file extension.
    $info = pathinfo($file->uri);
    $extension = $info['extension'];

    // Prepare new file name without extension.
    $new_file_name = webform_replace_tokens($component['extra']['rename'], $node, $submission, NULL, TRUE);
    $new_file_name = trim($new_file_name);
    $new_file_name = _webform_transliterate($new_file_name);
    $new_file_name = str_replace('/', '_', $new_file_name);
    $new_file_name = preg_replace('/[^a-zA-Z0-9_\\- ]/', '', $new_file_name);
    if (strlen($new_file_name)) {

      // Prepare the new uri with new filename.
      $destination = "{$destination_dir}/{$new_file_name}.{$extension}";

      // Compare the uri and Rename the file name.
      if ($file->uri != $destination) {
        file_move($file, $destination, FILE_EXISTS_RENAME);
      }
    }
  }
}