-1

私はfgets()間違った使い方をしていると思います。PHPファイルを開いて、そのファイル内の行を作成した変数と一致させようとしています。行が一致する場合は、その行のすぐ下のファイルに PHP コードを書き込み/挿入します。例:

function remove_admin(){
    $findThis = '<tbody id="users" class="list:user user-list">';
    $handle = @fopen("../../fns-control/users.php", "r"); // Open file form read.

    if ($handle) {
        while (!feof($handle)) // Loop til end of file.
        {
            $buffer = fgets($handle, 479); // Read a line.
            if ($buffer == '<tbody id="users" class="list:user user-list">') // Check for string.
            {

ここで、ファイルの 480 行目から PHP コードを書きたいと思います。どうすればよいでしょうか?

有用な情報は次のとおりです。IIS 6 および PHP 5.2。

4

1 に答える 1

3

これを試して:

<?php
function remove_admin(){
     $path = "../../fns-control/users.php";
     $findThis = '<tbody id="users" class="list:user user-list">';
     $phpCode = '<?php echo \'hello world\'; ?>';

     #Import file to string
     $f = file_get_contents($path);

     #Add in the PHP code
     $newfile = str_replace($findThis, $findThis . $phpCode, $f);

     #Overwrite the existing file
     $x = fopen($path, 'w');
     fwrite($x, $newfile);
     fclose($x);
 }
于 2011-01-27T22:53:50.803 に答える