doubeiji2602 2017-03-04 08:19
浏览 6
已采纳

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

So i'm currently working on a website on which the user can upload a file (e.g. myfile.html). Now i want the upload script to search for multiple strings (from an array) in this file, before moving it to the upload-directory.

As soon as it found string from the array in the file for the first time, it should not keep searching but tell the user about this and save the file (inaccessible of course), so i can look into it and may set it free by hand, if it was a "false positive".

This is what i've got so far:

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

So obviously this would just replace all the forbidden strings in the file and output the new file with them replaced by nothing.

But i'd like it to do something like this:

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

I hope you understand what i mean and can help me?

NOTE: The uploaded files would obviously not just contain exactly one of the strings in a line. They may look like this: <p>thisisnotallowed</p> or something.

  • 写回答

1条回答 默认 最新

  • dttnb997315 2017-03-04 08:52
    关注

    Use file_get_content() to read a file into a string

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

    Use strpos() to search a string for another string

    foreach($forbidden as $one) {
       $pos = strpos($uploaded_file, $one);
       if ($pos !== false) {
           echo('found a forbidden string in your file');
           $new_filename = $filename.'.lookinto';
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行