From 6f94e51ba6b5f120422a097af74572843e23139b Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 31 Mar 2019 21:39:19 -0400 Subject: [PATCH] Add Strings::sanitizeFilePathItem method --- src/Util/Strings.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Util/Strings.php b/src/Util/Strings.php index d6583b9c6..b2b710d96 100644 --- a/src/Util/Strings.php +++ b/src/Util/Strings.php @@ -375,4 +375,20 @@ class Strings )* )@'; } + + /** + * Ensures a single path item doesn't contain any path-traversing characters + * + * @see https://stackoverflow.com/a/46097713 + * @param string $pathItem + * @return string + */ + public static function sanitizeFilePathItem($pathItem) + { + $pathItem = str_replace('/', '_', $pathItem); + $pathItem = str_replace('\\', '_', $pathItem); + $pathItem = str_replace(DIRECTORY_SEPARATOR, '_', $pathItem); // In case it does not equal the standard values + + return $pathItem; + } }