$file->createNewFilename($name, $safeName = true)

Generates a new filename for a given name and makes sure to handle badly given extensions correctly

  • $name (string)
    New filename
  • $safeName (boolean)
    Whether to also convert to a URL safe filename
  • return (string)

Example

// this is an unvalidated filename coming from the user
$newName = 'something.exe';

// generate new filename for the existing file that is supposed to be renamed
$safeName = $page->file()->createNewFilename($newName);

// $safeName is 'something.jpg' because the current file is a JPG

Note: This method is automatically applied when renaming using $file->rename().