use Symfony\Component\Filesystem\Filesystem; $fs = new Filesystem(); $file = '/path/to/file.txt'; $targetDir = '/path/to/target/dir'; $relativePath = $fs->makePathRelative($targetDir, $file); // $relativePath now contains something like "../target/dir"
use Symfony\Component\Filesystem\Filesystem; $fs = new Filesystem(); $basePath = '/path/to/site/root'; $filePath = '/path/to/site/root/css/styles.css'; $relativeUrl = $fs->makePathRelative($filePath, $basePath); // $relativeUrl now contains something like "css/styles.css"The Symfony Filesystem Component is part of the Symfony framework, but it can also be used as a standalone package in other PHP projects.