There are several different solutions. If there can only be a file name, basename () will be used.
However, if this may be the way, a more complex solution is required.
//assume current directory, but can be set anything. Absolute path of course $basedir = dirname(__FILE__); //assume our files are below document root. //Otherwise use it root dir instead of DOCUMENT_ROOT $filename = realpath($_SERVER['DOCUMENT_ROOT'].$_GET['file']); if (substr($filename,0,strlen($basedir)) !== $basedir) { header ("HTTP/1.0 403 Forbidden"); exit; }
there is also a useful PHP configuration option open_basedir
Your common sense
source share