PHPackages                             rayne/virtual-path - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. rayne/virtual-path

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

rayne/virtual-path
==================

The VirtualPath library normalises paths and prevents directory traversal attacks without querying a file system.

1.0.0(8y ago)872MITPHPPHP &gt;=5.6

Since Oct 10Pushed 6y ago2 watchersCompare

[ Source](https://github.com/Rayne/virtual-path)[ Packagist](https://packagist.org/packages/rayne/virtual-path)[ RSS](/packages/rayne-virtual-path/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

Rayne\\VirtualPath
==================

[](#raynevirtualpath)

The `VirtualPath` library normalises paths and prevents directory traversal attacks without querying a file system.

[![Latest Stable Version](https://camo.githubusercontent.com/068db53c674890a55db0c1138acfb650d4d704afe189f14e7a7b780f70c9b08c/68747470733a2f2f706f7365722e707567782e6f72672f7261796e652f7669727475616c2d706174682f762f737461626c65)](https://packagist.org/packages/rayne/virtual-path)[![Code Coverage](https://camo.githubusercontent.com/38f5b21c2e747647f02036a762a8df6a2262614040bf6dc24ad142b9fea2567d/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7261796e652f7669727475616c2d706174682f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/rayne/virtual-path/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/63b666b033da3129df0d102603e3c0a55d317d234c78fdb9ce1f8b483f5fb876/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7261796e652f7669727475616c2d706174682f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/rayne/virtual-path/?branch=master)[![License](https://camo.githubusercontent.com/d6a45609fc909f3cc7af249558f93b472ffe40bf4f8b1fe2f67c92abf43711b3/68747470733a2f2f706f7365722e707567782e6f72672f7261796e652f7669727475616c2d706174682f6c6963656e7365)](https://packagist.org/packages/rayne/virtual-path)

Contents
--------

[](#contents)

- [Installation](#installation)
- [Dependencies](#dependencies)
- [Usage](#usage)
- [Examples](#examples)
- [Implementation Details](#implementation-details)
- [Tests](#tests)

Installation
------------

[](#installation)

It's recommended to use the dependency manager [Composer](https://getcomposer.org/download)to install `rayne/virtual-path`.

```
composer require rayne/virtual-path
```

Dependencies
------------

[](#dependencies)

- PHP 5.6 or better

Usage
-----

[](#usage)

The `VirtualPath` class normalises inputs to absolute virtual paths without querying any file system. It also detects and flags directory traversal attacks.

The `JailedPath` class utilises `VirtualPath` to build safe paths which can be used for working with real files. The normalisation is done relative to a `jail` called path which is used as virtual root for any path entered by the user. As `JailedPath` does not query the file system it's suited for working with local, remote or fictional paths.

Please read the [Implementation Details](#implementation-details) section for more details.

**TL;DR** *Use the `JailedPath` class when in doubt.*

Examples
--------

[](#examples)

### `JailedPath`

[](#jailedpath)

In this example website visitors are allowed to download any file from the local directory `/test`by specifying the relative path as `GET` parameter. To prevent users from leaving the directory with directory traversal attacks, `JailedPath` is used with `/test` as the virtual root directory.

```
