从数组中搜索字符串的文件

时间:2017-03-04 08:19:36

标签: php arrays file

所以我目前正在开发一个用户可以上传文件的网站(例如myfile.html)。 现在我希望上传脚本在将此文件移动到upload-directory之前搜索此文件中的多个字符串(来自数组)。

一旦它第一次从文件中的数组中找到字符串,它就不应该继续搜索,而是告诉用户这个并保存文件(当然不可访问),所以我可以查看它并可能设置如果它是“假阳性”,它可以手动释放。

这是我到目前为止所得到的:

$forbidden = array('thisisnotallowed', 'thisisntaswell', 'alsonotallowed');
$uploaded_file = file($_FILES['fileupload']['name']);
$deleted = str_ireplace($forbidden, "", $uploaded_file);
foreach($deleted as $line) {
    echo($line.'<br>');
}

显然,这只会替换文件中的所有禁用字符串,并输出新文件,并将它们替换为空。

但我希望它能做到这样的事情:

if(find_string($uploaded_file, $forbidden)) {
    echo('found a forbidden string in your file');
    $new_filename = $filename.'.lookinto';
}

我希望你明白我的意思并能帮助我吗?

注意:上传的文件显然不仅仅包含一行中的一个字符串。它们可能如下所示:<p>thisisnotallowed</p>或其他。

1 个答案:

答案 0 :(得分:0)

使用file_get_content()将文件读入字符串

$uploaded_file = file_get_content($_FILES['fileupload']['name']);

使用strpos()在字符串中搜索另一个字符串

foreach($forbidden as $one) {
   $pos = strpos($uploaded_file, $one);
   if ($pos !== false) {
       echo('found a forbidden string in your file');
       $new_filename = $filename.'.lookinto';
   }
}
相关问题