Skip to content

Instantly share code, notes, and snippets.

@jehoshua02
Created October 16, 2011 10:13
Show Gist options
  • Save jehoshua02/1290734 to your computer and use it in GitHub Desktop.
Save jehoshua02/1290734 to your computer and use it in GitHub Desktop.
Restricting file access using realpath
<?php
$file = pathinfo($_GET['form']);
$file = realpath($file['dirname']) . DIRECTORY_SEPARATOR . $file['basename'];
if (empty($file))
{
die("No file requested.");
}
$invalid = !(preg_match('/^' . preg_quote(realpath('admin/upload')) . '/', $file));
if ($invalid)
{
die("Invalid file path for {$file}");
}
if (!file_exists($file))
{
die("Could not find file {$file}.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment