douren2395 2017-04-30 16:17
浏览 78

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 :(

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 arduino控制ps2手柄一直报错
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题
    • ¥15 Visual Studio问题
    • ¥20 求一个html代码,有偿
    • ¥100 关于使用MATLAB中copularnd函数的问题