我有一个位于/var/log/myscript.php的php脚本,我想从/ home / user / run执行它。我在下面不断收到以下错误。但是,如果我直接进入文件夹并运行php myscript.php,脚本将执行。奇怪,任何建议。我已经包含了以下脚本
PHP注意:未定义的变量:第73行的/var/log/myscript.php中的f PHP警告:fclose()期望参数1为资源,在第73行的/var/log/myscript.php中给出null
<?php
$host = "some_destination";
$user = "postgres";
$pass = "postgres";
$db = "some_database";
$con = pg_connect("host=$host dbname=$db user=$user password=$pass");
$line = 1;
$files = array();
$dir = opendir('.');
while(false != ($file = readdir($dir))) {
if(($file != ".") and ($file != ".."){
$files[] = $file;.
}
}
natsort($files); // sort.
foreach($files as $file){
if ( strpos($file,'syslog.1') !== FALSE )
{
echo "\n" .$file;
$f = fopen($file,"r") or exit("Unable to open file!");
$arr_to_insert = array();
while (!feof($f)) {
$group[] = fgets($f);
$line = preg_replace('!\s+!',' ', $group[0]);
$arrM = explode(' ',$line);
$month = $arrM[0];
$day = $arrM[1];
$timestp = $arrM[2];
$src = str_replace("SRC=","",$arrM[11]);
$dst = str_replace("DST=","",$arrM[12]);
$spt = str_replace("SPT=","",$arrM[19]);
$dpt = str_replace("DPT=","",$arrM[20]);
if (ctype_digit($day)) {
if (ctype_digit($spt)){
if (ctype_digit($dpt)){
$sql = "INSERT INTO
logs(logmonth,logday,timestp,src,dst,spt,dpt)
VALUES('$month','$day','$timestp','$
$src','$dst','$spt','$dpt')";
$res = pg_query($con, $sql) or die("Cannot execute query: $sql\n");
$ar = pg_affected_rows($res);
}
}
}
unset($group);
}
}
}
pg_close($con);
fclose($f);
?>