Back to InstallerHelper class

Method getFilenameFromUrl

public static string
getFilenameFromUrl
(mixed $url)
Gets a file name out of a url
Parameters
  • string $url URL to get name from
Returns
  • string Clean version of the filename or a unique id
Since
  • 3.1

Method getFilenameFromUrl - Source code

/**
 * Gets a file name out of a url
 *
 * @param   string  $url  URL to get name from
 *
 * @return  string  Clean version of the filename or a unique id
 *
 * @since   3.1
 */
public static function getFilenameFromUrl($url)
{
    $default = uniqid();
    if (!\is_string($url) || strpos($url, '/') === false) {
        return $default;
    }
    // Get last part of the url (after the last slash).
    $parts = explode('/', $url);
    $filename = array_pop($parts);
    // Replace special characters with underscores.
    $filename = preg_replace('/[^a-z0-9\\_\\-\\.]/i', '_', $filename);
    // Replace multiple underscores with just one.
    $filename = preg_replace('/__+/', '_', trim($filename, '_'));
    // Return the cleaned filename or, if it is empty, a unique id.
    return $filename ?: $default;
}