Mr.Shawn Posted March 30, 2008 Share Posted March 30, 2008 Hello guys, I'm trying to do a checking of the filename provided such that it is not a CGI scripts. How do I check it with preg_match? Currently I do have the following for checking php but I wonder how can I check for other extensions such as .js, .html, etc. if (preg_match('/php/i', substr($file, -3))) { die("PHP file not downloadable"); exit; } Link to comment https://forums.phpfreaks.com/topic/98594-cgi-scripts-preg_match/ Share on other sites More sharing options...
dsaba Posted March 31, 2008 Share Posted March 31, 2008 preg_match('~\.cgi$~im', $string); Really I would check the mime type or file type with some other php function, or check the headers of the file itself by reading it, people can still change their extensions to whatever they want, but the content (headers) of the file never lies. Link to comment https://forums.phpfreaks.com/topic/98594-cgi-scripts-preg_match/#findComment-505576 Share on other sites More sharing options...
Mr.Shawn Posted April 1, 2008 Author Share Posted April 1, 2008 preg_match('~\.cgi$~im', $string); Hi dsaba, the regex doesn't works tho. It still allows php file to be downloaded. Link to comment https://forums.phpfreaks.com/topic/98594-cgi-scripts-preg_match/#findComment-506215 Share on other sites More sharing options...
dsaba Posted April 1, 2008 Share Posted April 1, 2008 I hope you realize I don't know what you mean when you say "doesn't work". Link to comment https://forums.phpfreaks.com/topic/98594-cgi-scripts-preg_match/#findComment-506767 Share on other sites More sharing options...
Mr.Shawn Posted April 2, 2008 Author Share Posted April 2, 2008 It means that the code you provided is not working and it does not validate CGI scripts? Link to comment https://forums.phpfreaks.com/topic/98594-cgi-scripts-preg_match/#findComment-507141 Share on other sites More sharing options...
lordfrikk Posted April 2, 2008 Share Posted April 2, 2008 What about: <?php $p = pathinfo($file); if ($p['extension'] == 'cgi'): die("PHP file not downloadable"); exit; endif; ?> Link to comment https://forums.phpfreaks.com/topic/98594-cgi-scripts-preg_match/#findComment-507263 Share on other sites More sharing options...
Mr.Shawn Posted April 2, 2008 Author Share Posted April 2, 2008 Looks like that's the only way. Thanks lordfrikk. Link to comment https://forums.phpfreaks.com/topic/98594-cgi-scripts-preg_match/#findComment-507427 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.