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';
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算