Created
October 16, 2011 10:13
-
-
Save jehoshua02/1290734 to your computer and use it in GitHub Desktop.
Restricting file access using realpath
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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