定义文件位置数组,解析和替换.这是我的错误


Define array of file locations, parse and replace. Where's my error?

我试图用文件url列表定义一个数组,然后对每个文件进行解析,如果找到预定义的字符串,则替换该字符串。由于某种原因,我有不工作,我不确定什么是不正确的:

<?php
$htF = array('/home/folder/file.extension', '/home/folder/file.extension', '/home/folder/file.extension', '/home/folder/file.extension', '/home/folder/file.extension');
function update() {
global $htF;
$handle = fopen($htF, "r");
if ($handle) {
     $previous_line = $content = '';
     while (!feof($handle)) {
          $current_line = fgets($handle);
          if(stripos($previous_line,'PREDEFINED SENTENCE') !== FALSE)
          {
               $output = shell_exec('URL.COM');
               if(preg_match('#([0-9]{1,3}'.){3}[0-9]{1,3}#',$output,$matches))
               {
                    $content .= 'PREDEFINED SENTENCE '.$matches[0]."'n";
               }
          }else{
               $content .= $current_line;
          }
          $previous_line = $current_line;
     }
     fclose($handle);
     $tempFile = tempnam('/tmp','allow_');
     $fp = fopen($tempFile, 'w');
     fwrite($fp, $content);
     fclose($fp);
     rename($tempFile,$htF);
     chown($htF,'admin');
     chmod($htF,'0644');
}
}
array_walk($htF, 'update');
?>

任何帮助将是非常感激的!

  • 您有打开文件的权限吗?
  • 你有权限写/tmp吗?
  • 您是否有权限写入目标文件或文件夹?
  • 你有权限去镇上吗?
  • 你检查过你的正则表达式了吗?试试http://regexpal.com/之类的东西看看是否有效。

尝试为所有失败条件添加错误消息或抛出异常。

有这一行:

 if(stripos($previous_line,'PREDEFINED SENTENCE') !== FALSE)

,我认为你只需要一个!=。是吗?

您在更新函数中使用$htF作为全局,这意味着您正在尝试fopen()一个数组。

$fh = fopen($htF, 'r');

将被解析为

$fh = fopen('Array', 'r');

并返回false,除非你碰巧有一个名为'Array'的文件。

你也没有为你的函数指定任何参数,所以array_walk不能传入它当时正在处理的数组元素