我正在尝试定义一个包含文件urls列表的数组,然后解析每个文件,如果找到预定义的字符串,则替换该字符串。由于某些原因,我的东西不能工作,我不确定什么是不正确的:
<?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');
?>任何帮助都将不胜感激!
发布于 2011-08-16 22:17:21
尝试添加错误消息或为所有这些失败条件抛出异常。
发布于 2011-08-16 22:14:45
有这样一句话:
if(stripos($previous_line,'PREDEFINED SENTENCE') !== FALSE)我想你只是想要一个!=在那里。是?
发布于 2011-08-16 22:34:55
您在fopen()函数中使用$htF作为全局更新,这意味着您正在尝试更新一个数组。
$fh = fopen($htF, 'r');将被解析为
$fh = fopen('Array', 'r');并返回false,除非您碰巧有一个名为'Array‘的文件。
您还没有为您的函数指定任何参数,所以array_walk不能传入它当时正在处理的数组元素。
https://stackoverflow.com/questions/7079504
复制相似问题