[英]preg_match for <?php, <?, and/or ?>
I'm not very familiar with regEx's and I'm trying to find a preg_match regex for searching for any of the following strings within a file and if found it will halt it. 我对regEx不太熟悉,我正在尝试查找preg_match regex,以在文件中搜索以下任何字符串,如果找到则将其暂停。 I already have the fopen
and fgets
and fclose
setup I just need to use a regex inside of a preg_match for the following php tags: 我已经有了fopen
和fgets
和fclose
设置,我只需要在preg_match中使用以下php标签的正则表达式即可:
<?php
<?
<?php
<?
?>
so if preg_match returns 1 than this will skip this file and not upload it. 因此,如果preg_match返回1,则将跳过此文件而不上传。 I am using the $_FILES array to upload it via post, so I'm hoping I can use the $_FILES['file']['tmp_name'] variable for this to read through the file. 我正在使用$ _FILES数组通过发布上传它,所以我希望可以使用$ _FILES ['file'] ['tmp_name']变量来读取文件。
Thanks for your help with this :) 感谢您的帮助:)
EDIT 编辑
if (in_array('application/x-httpd-php', $files[$filid]['mimetypes']) && ($_FILES[$value]['type'][$n] == 'application/octet-stream' || $_FILES[$value]['type'][$n] == 'application/octetstream'))
{
$file_extension = strtolower(substr(strrchr($_FILES[$value]['name'][$n], '.'), 1));
if ($file_extension == 'php')
{
// Reading the current php file to make sure it's a PHP File.
$fo = fopen($_FILES[$value]['tmp_name'][$n], 'rb');
while (!feof($fo))
{
$fo_output = fgets($fo, 16384);
// look for a match
if (preg_match([REG EX HERE], $fo_output) == 1)
{
$php = true;
break;
}
}
fclose($fo);
}
}
OK, I apologize, but actually, what I am doing is I need to find a PREG MATCH. 好的,我很抱歉,但是实际上,我要做的是找到一个PREG MATCH。 Because if it is a PHP FILE, I need to set the MIME TYPE to: application/x-httpd-php within the database. 因为如果是PHP FILE,则需要将MIME TYPE设置为:数据库中的application / x-httpd-php。 BECAUSE I'm also allowing PHP Files to be uploaded as well in certain instances. 因为我还允许在某些情况下也上传PHP文件。 So hopefully the code I posted above makes more sense to you all now. 因此,希望我上面发布的代码现在对大家都有意义。
Can someone please help me with a preg_match
regex for this please? 有人可以帮我准备preg_match
正则表达式吗?
/(?:<\?(?!xml)|\?>)/
(15个字符)
If you want to parse the file, try the following instead: 如果要解析文件,请尝试以下操作:
function containsPhp($file) {
if(!$content = file_get_contents($file)) {
trigger_error('Not a file');
return false;
}
foreach(token_get_all($content) as $token) {
if(is_array($token) && in_array(current($token), array(T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO))) {
return true;
}
}
return false;
}
... besides checking for a php extension (php, php5, phtml, inc etc). ...除了检查php扩展名(php,php5,phtml,inc等)。
\?>|<\?((?=php)|(?!\w))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.