Skip to content

Instantly share code, notes, and snippets.

@dlueth
Created June 27, 2013 21:28
Show Gist options
  • Save dlueth/5880557 to your computer and use it in GitHub Desktop.
Save dlueth/5880557 to your computer and use it in GitHub Desktop.
PHP validator that checks if a given string is a valid/allowed filesystem path across operating systems
function isValidPath($value) {
return (preg_match('/^(\/(?:(?:(?:(?:[a-zA-Z0-9\\-_.!~*\'():\@&=+\$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*)(?:;(?:(?:[a-zA-Z0-9\\-_.!~*\'():\@&=+\$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*))*)(?:\/(?:(?:(?:[a-zA-Z0-9\\-_.!~*\'():\@&=+\$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*)(?:;(?:(?:[a-zA-Z0-9\\-_.!~*\'():\@&=+\$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*))*))*))$/', (string) $value) > 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment