php - block access to file while using opendir/openfile etc -
i want deny access file data.xml functions file_exists/opendir/openfile etc
my file test.php contains:
$path="./mydata.html"; if ($handle = opendir($path)) { } closedir($handle);
how make unreadable without blocking functions? possible?
- .htaccess?
- should block file via chmod set different user execute php script?
- other way?
thanks
the easiest way write function check filepath, , call (i.e.) sanitize_filepath($path)
whenever accept user data or before every fopen()
/opendir()
/file_get_contents()
. in sanitize_filepath()
function, explode $path
on path_separator
, use in_array()
check internal blacklist against provided path.
return false or throw exception if don't watch continue, filepath or true if do.
preventing access via .htaccess means cannot go http://example.com/your/website/data.xml , see file. still potentially access file exploiting php script if requested http://example.com/your/website/script.php?file=data.xml, assuming script.php <?= echo file_get_contents($_get['file']); ?>
. (this useless , insecure script, it's example.)
preventing access via chmodding permissions prevent php reading file. if don't need read file, shouldn't in place php can access it-- move outside of webroot.
Comments
Post a Comment