You are here

protected function Textimage::createFilename in Textimage 8.3

Same name and namespace in other branches
  1. 8.4 src/Textimage.php \Drupal\textimage\Textimage::createFilename()

Creates a full file path from a directory and filename.

Copied parts of file_create_filename() to avoid file existence check.

Parameters

string $basename: String filename.

string $directory: String containing the directory or parent URI.

Return value

string File path consisting of $directory and a unique filename based off of $basename.

1 call to Textimage::createFilename()
Textimage::setTargetUri in src/Textimage.php
Set image destination URI.

File

src/Textimage.php, line 769

Class

Textimage
Provides a Textimage.

Namespace

Drupal\textimage

Code

protected function createFilename($basename, $directory) {

  // Strip control characters (ASCII value < 32). Though these are allowed in
  // some filesystems, not many applications handle them well.
  $basename = preg_replace('/[\\x00-\\x1F]/u', '_', $basename);
  if (substr(PHP_OS, 0, 3) == 'WIN') {

    // These characters are not allowed in Windows filenames.
    $basename = str_replace([
      ':',
      '*',
      '?',
      '"',
      '<',
      '>',
      '|',
    ], '_', $basename);
  }

  // A URI or path may already have a trailing slash or look like "public://".
  if (substr($directory, -1) == '/') {
    $separator = '';
  }
  else {
    $separator = '/';
  }
  return $directory . $separator . $basename;
}