file_create_filename

  1. drupal
    1. drupal6
    2. drupal7
Versionen
drupal6 – drupal7 file_create_filename($basename, $directory)

Create a full file path from a directory and filename.

If a file with the specified name already exists, an alternative will be used.

Übergabeparameter

$basename String filename

$directory String directory

Rückgabewert

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

Verwandte Themen

▾ 2 functions call file_create_filename()

FileDirectoryTest::testFileCreateNewFilepath in modules/simpletest/tests/file.test
This will take a directory and path, and find a valid filepath that is not taken by another file.
file_destination in includes/file.inc
Determines the destination path for a file depending on how replacement of existing files should be handled.

Code

includes/file.inc, line 700

<?php
function file_create_filename($basename, $directory) {
  $destination = $directory . '/' . $basename;

  if (file_exists($destination)) {
    // Destination file already exists, generate an alternative.
    $pos = strrpos($basename, '.');
    if ($pos !== FALSE) {
      $name = substr($basename, 0, $pos);
      $ext = substr($basename, $pos);
    }
    else {
      $name = $basename;
      $ext = '';
    }

    $counter = 0;
    do {
      $destination = $directory . '/' . $name . '_' . $counter++ . $ext;
    } while (file_exists($destination));
  }

  return $destination;
}
?>

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen