PHP以递归方式搜索文件并在每个上执行函数

I am trying to create a PHP script that is supposed to recursively search for all files in filesystem by a name, return results and add 1 line into each selected file.

Here is what I have now: 1. Part which is looking for files, it is working fine:

    $search = "test.txt";

    $dir_iterator = new RecursiveDirectoryIterator("../");
    $iterator = new RecursiveIteratorIterator($dir_iterator, 
    RecursiveIteratorIterator::SELF_FIRST);

    foreach ($iterator as $file) {
        if($file->getFilename() == $search){
            echo "<input type=\"checkbox\" name=\"optim[]\" value=\"$file\">
".$file."<br>";
        }
    }
    ?>
    <input type="submit" value="Add to selected">
    </fieldset>
    </form>

This part works and returns a form with checkboxes. And here is a part which is supposed to create a backup of and add a line to selected:

    $newfile = "text.txt$date";
    $date = date('m-d-Y-H:i:s');
    $target = "Yesterday was rainy";
    $newline = "But today the sun is shining";

    function update() { 
        foreach( $_POST as $fin ) {
            if (!copy($fin, $newfile)) { 
            echo "failed to copy"; 
        }

        $stats = file($fin, FILE_IGNORE_NEW_LINES);   
        $offset = array_search($target,$stats) +1;
        array_splice($stats, $offset, 0, $newline);   
        file_put_contents($fin, join("
", $stats));   

        if($newline) {
            echo "All file updated";
        } else {
            echo "Sorry, I was unable to add :(";
        }
    }

But when a button is clicked and the function is called with

    if($_SERVER['REQUEST_METHOD']=='POST')
    {
    update();
    } 

I get:

Warning: copy() expects parameter 1 to be a valid path, array given in itworked.php on line 51 failed to copy

Warning: file() expects parameter 1 to be a valid path, array given in itworked.php on line 54

Warning: array_search() expects parameter 2 to be array, null given in itworked.php on line 55

Warning: array_splice() expects parameter 1 to be array, null given in itworked.php on line 56

Warning: join(): Invalid arguments passed in itworked.php on line 57

Warning: file_put_contents() expects parameter 1 to be a valid path, array given in itworked.php on line 57

It looks like $fin is an array and my function can not be executed on it, but I do not understand why - shouldn't foreach work on elements of _POST?

I am a total rookie, newbie and a dummie, so any help would be greatly appreciated. Thank you.

UPD: sure, it is called with "update", and obviously not related.

UPD2: I tried changing foreach( $_POST as $fin ) with foreach( $_POST['optim'] as $fin )

and this solved most of the errors, but it looks like the function still does not recognize filenames. Here is the error:

Warning: copy(): Filename cannot be empty in itworked.php on line 50 failed to copy

And the 50 line is: if (!copy($fin, $newfile))

UPD3:

Hi. Per @quasimodos-clone suggestion I tried echoing $fin and got something like this:

../dir/test.txt

and the current working directory is

/files/testing/

So I though that it may be the cause. So I tried using $_SERVER['DOCUMENT_ROOT'] which does not include files in /files/otherfolder, only /files/testing/, but at least gives something.

This changed the output of $fin to:

/files/testing/dir/test.txt

But still when I try to add the line by submitting, I get the same error:

Warning: copy(): Filename cannot be empty in /files/testing/itworked.php

The line is:

   if (!copy($fin, $newfile)) { 

Have no idea what to try next :(

dongmingxiang0312
dongmingxiang0312 也许是因为你的迭代器是基于“../”?输出$fin和getcwd()
3 年多之前 回复
dongque1462
dongque1462 很抱歉没有提供足够的信息。代码行是if(!copy($fin,$newfile)){我不知道为什么它看不到文件名。
3 年多之前 回复
doujiangao4229
doujiangao4229 还有什么不清楚的?
3 年多之前 回复
doubeng9407
doubeng9407 谢谢,@u_mulder,当然,我称之为“更新”,这只是我在发布这个问题时犯的一个愚蠢的错误。我尝试了foreach($_POST['optim'],这解决了大部分问题(非常感谢你!),但现在我得到了:警告:copy():在第51行的itworked.php中,文件名不能为空复制
3 年多之前 回复
dongnunai3125
dongnunai3125 但是显而易见,你需要将foreach($_POSTas$fin)更改为foreach($_POST['optim']为$fin)
3 年多之前 回复
douju4278
douju4278 调用optimize()但描述update()?
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问