yii\helpers\BaseFileHelper::normalizePath PHP Method

normalizePath() public static method

The normalization does the following work: - Convert all directory separators into DIRECTORY_SEPARATOR (e.g. "\a/b\c" becomes "/a/b/c") - Remove trailing directory separators (e.g. "/a/b/c/" becomes "/a/b/c") - Turn multiple consecutive slashes into a single one (e.g. "/a///b/c" becomes "/a/b/c") - Remove ".." and "." based on their meanings (e.g. "/a/./b/../c" becomes "/a/c")
public static normalizePath ( string $path, string $ds = DIRECTORY_SEPARATOR ) : string
$path string the file/directory path to be normalized
$ds string the directory separator to be used in the normalized result. Defaults to `DIRECTORY_SEPARATOR`.
return string the normalized file/directory path
    public static function normalizePath($path, $ds = DIRECTORY_SEPARATOR)
    {
        $path = rtrim(strtr($path, '/\\', $ds . $ds), $ds);
        if (strpos($ds . $path, "{$ds}.") === false && strpos($path, "{$ds}{$ds}") === false) {
            return $path;
        }
        // the path may contain ".", ".." or double slashes, need to clean them up
        $parts = [];
        foreach (explode($ds, $path) as $part) {
            if ($part === '..' && !empty($parts) && end($parts) !== '..') {
                array_pop($parts);
            } elseif ($part === '.' || $part === '' && !empty($parts)) {
                continue;
            } else {
                $parts[] = $part;
            }
        }
        $path = implode($ds, $parts);
        return $path === '' ? '.' : $path;
    }

Usage Example

Esempio n. 1
0
 /**
  * Verifica si existe un directorio, si no existe lo crea
  * @param $directorio
  * @throws \yii\base\Exception
  */
 public static function existeDirectorio($directorio)
 {
     $base = BaseFileHelper::normalizePath(Yii::getAlias('@libreria') . '/imagenes/' . $directorio);
     if (!file_exists($base)) {
         BaseFileHelper::createDirectory($base, 0750, true);
     }
 }
All Usage Examples Of yii\helpers\BaseFileHelper::normalizePath