Hi,
I have a major problem that I can not figure out. I have a directory that is updated once a night by fetching data from a site provider. For the last 3 days it will go fetch the data and fills the log file with the following errors over and over until the file completely fills the webhosting space and locks up the email and functions.

[01-Oct-2009 00:18:08] PHP Warning: feof(): supplied argument is not a valid stream resource in /home/selkirka/public_html/import/import.php on line 126
[01-Oct-2009 00:18:08] PHP Warning: fgets(): supplied argument is not a valid stream resource in /home/selkirka/public_html/import/import.php on line 127
[01-Oct-2009 00:18:08] PHP Warning: feof(): supplied argument is not a valid stream resource

The complete import.php file is this:

#!/usr/local/bin/php
<?
require("db.inc.php");
require("col2int.php");
$db_table="test_users";


$real=0; $filekey="";
$options = getopt("f:d:cv:rk:");
if (  isset($options['r']) ) $real=1;
if (  isset($options['v']) ) $verbose=trim($options['v']);
if (  isset($options['d']) ) $debug=trim($options['d']);
if (  isset($options['f']) ) $filename=trim($options['f']);
if (  isset($options['k']) ) $filekey=trim($options['k']);
if ( $real == 1 ) $db_table="pmd_users";


// filekey now from the commandline
// preg_match("/_(.*?)\./",$filename,$res);
// $filekey=$res[1];
switch ($filekey) {
    case "users";
	$filespec=1; $keynum=a; $keyfield="selector";
    break;
    case "org";
	$filespec=2; $keynum=a; $keyfield="selector";
    break;
/*
    case "off";
    case "affoff";
	$filespec=2; $keynum=103; $keyfield="selector";
    break;
*/
    default:
	print "unknown filekey $filekey\n"; exit(2);
}

/*
$category['rea']="2#0#0"; // rea
$category['aff']="3#0#0"; // spons+aff
$category['affoff']="3#0#0"; 
$category['app']="4#0#0";
$category['off']="5#0#0";
*/

$category[1]['11']="2#0#0";
$category[1]['1']="2#0#0";
$category[1]['2']="2#0#0";
$category[1]['7']="2#0#0";
$category[1]['8']="2#0#0";
$category[1]['14']="2#0#0";

$category[1][3]="4#0#0";
$category[1][5]="4#0#0";

$category[1][4]="3#0#0";
$category[1][9]="3#0#0";
$category[1][12]="3#0#0";
$category[1][15]="3#0#0";
$category[1][16]="3#0#0";

$category[2]['5']="3#0#0";
$category[2]['7']="5#0#0";
$category[2]['2']="5#0#0";



$states['ID']=2;
$states['WA']=3;
$states['MT']=4;


$db=mysql_connect($db_host,$db_user,$db_password);
mysql_select_db($db_dbname);

$sql="select * from pmd_location";
$res=mysql_query($sql);
while ( $row=mysql_fetch_array($res,MYSQL_BOTH) ) {
    $loc_code[strtolower($row['location'])]=$row['locationselector'];
}

$assigns[1]=array(
d=>'firmname,manager',
f=>'address',
l=>'phone',
r=>'fax',
p=>'mobile',
u=>'mail',
v=>'www',
99=>'category',
h=>'city',
i=>'state',
j=>'zip',
a=>'selector,login',
b=>'reserved_3',
104=>'location',
111=>'business'
);

// d=>'firmname,business',
$assigns[2]=array(
d=>'firmname',
e=>'address',
k=>'phone',
m=>'mobile',
o=>'fax',
p=>'mail',
q=>'www',
99=>'category',
g=>'city',
h=>'state',
i=>'zip',
a=>'selector,login',
104=>'location'
);

if ( $verbose > 0 ) print "Working on $filename ...\n";
$sql="update $db_table set reserved_1 = 'OLD' where reserved_2='";
$sql .= $filekey . "'";
if ( $verbose > 0 ) print "Counting existing records: ";
$res=mysql_query($sql);
if ( $verbose > 0 ) print mysql_affected_rows() . "\n";

$linecount=0; $updates=0; $inserts=0; $errors=0;
$ha=fopen($filename,"r");
while ( !feof($ha) ) {
    $l=fgets($ha);
    $linecount++;
//     if ( $linecount == 1 ) continue;
    if ( trim($l) == "" ) continue;

    $fields=preg_split("/\t/",$l);

    switch($filespec) {
    case 1; // _app,_rea,_aff
	if ( $fields[x] != 1 ) { $skipped++; continue; }
        // secial case for city, state, zip
        $fields[i]=$states[strtoupper(trim($fields[i]))]; // state
        if ( $fields[i] == "" )  $fields[i] = 1;
	$oldjpg=strtolower(substr($fields[e],0,3) . substr($fields[d],0,3));
// print "OLDJPG[$oldjpg]\n";
	if ( ! @stat("../phpmydirectory/logo/$oldjpg.jpg") ) {
	    // print "notfound:[$oldjpg][" . $fields[e] . "][" . $fields[d] . "]\n";
	}

        $fields[f] = trim($fields[f]) . "\n" . trim($fields[g]); // two addr lines

        // $fields[d] = $fields[d] . " " . $fields[e]; // two  name fields
        $manager=$fields[d] . " " . $fields[e]; // two  name fields

        $fields[d] = $fields[e] . ", " . $fields[d]; // two  name fields
        if ( trim($fields[v]) && !preg_match("/^http:\/\//i",$fields[v]) ) {
	    $fields[v] = "http://" . $fields[v];
        }
	$fields[l] = phoneformat($fields[l]);
	$fields[p] = phoneformat($fields[p]);
	$fields[r] = phoneformat($fields[r]);
        $fields[99]=$category[$filespec][$fields[c]];
        $fields[104]=$loc_code[strtolower($fields[h])];
	$cmd=
	"cp ../phpmydirectory/logo/" . $oldjpg . ".jpg" .
	" ../phpmydirectory/logo_new/1_" . $fields[a] . ".jpg";
	// only for the first runs...: system($cmd);
	// $sql="select business from $db_table where category=\"3#0#0\" and selector='2_" . $fields[b] . "'";

	$sql="select firmname from $db_table where selector='2_" . $fields[b] . "'";
	$res=mysql_query($sql);
	$row=mysql_fetch_assoc($res);
	$fields[111]=$row['firmname'];
// print "FOUND [$sql][" .  $fields[111] . "]\n";
	if ( $fields[c] == 3 ||  $fields[c] == 8 ) {
	    $sql="update $db_table set business='$manager',manager='$manager'  where selector='2_" . $fields[b] . "'";
	    $res=mysql_query($sql);
// print "SETT[$manager] to [$sql]\n";
	}
        break;
    case 2;  // _off
        // secial case for city, state, zip
        $fields[e] = $fields[e] . "\n" . $fields[f]; // two addr lines

        $fields[h]=$states[strtoupper(trim($fields[h]))]; // state
        if ( $fields[h] == "" )  $fields[h] = 1;

	$fields[k] = phoneformat($fields[k]);
	$fields[m] = phoneformat($fields[m]);
	$fields[o] = phoneformat($fields[o]);

        if ( trim($fields[q]) && !preg_match("/^http:\/\//i",$fields[q]) ) {
	    $fields[q] = "http://" . $fields[q];
        }
        $fields[99]=$category[$filespec][$fields[b]];
        $fields[104]=$loc_code[strtolower($fields[g])];
	$oldjpg=$fields[c];
	if ( ! @stat("../phpmydirectory/logo/$oldjpg.jpg") ) {
	    // print "notfound:[$oldjpg][" . $fields[e] . "][" . $fields[d] . "]\n";
	}
	$cmd=
	"cp ../phpmydirectory/logo/" . $fields[c] . ".jpg" .
	" ../phpmydirectory/logo_new/2_" . $fields[a] . ".jpg";
	// only the first runs did this: system($cmd);
        break;
    }
    // common special fields
    $fields[$keynum]=$filespec . "_" . $fields[$keynum];
    if ( !$fields[99] ) { continue; }
    if ( $fields[104] == "" && $verbose > 0 ) {
	print "Unknown location: [" . $filekey . "/" . $fields[a] . "][" . $fields[($filespec == 1) ? h : g] . "]\n";
    } else {
        // print "Known location: [" . $fields[h] . "/" . $fields[g] . "][" . $fields[104] . "]\n";
    }

    $db="";
    $db .= "firmstate='on',";
    $db .= "flag='D',";

    foreach ( $assigns[$filespec] as $key=>$val ) {
	if ( $val == "" ) continue;
	$target_fields=preg_split("/,/",$val);
	foreach ( $target_fields as $target) {
	    $db .= $target . " = '" . mysql_escape_string(trim($fields[$key])) . "',";
	}
    }
 
    $sql_suffix=" where ";
    $sql_suffix .= $keyfield . " = '";
    $sql_suffix .= mysql_escape_string(trim($fields[$keynum])) . "'";
    $sql_prefix="update $db_table set ";
    $sql_action="updating " . trim($fields[$keynum]) . " ";
    $sql_state="UPDATED";

    $sql = "select * from  $db_table " . $sql_suffix;
    $res=mysql_query($sql);
    // print "==>$sql\n";
    if ( mysql_num_rows($res) == 0 ) {
	$sql_prefix="insert into $db_table set ";
	$sql_suffix="";
        $sql_action="inserting " . trim($fields[$keynum]) . " ";
	$sql_state="INSERTED";
    }

    $db .= "reserved_1='$sql_state',reserved_2='$filekey',";

    $sql = $sql_prefix . preg_replace("/,$/","",$db) . $sql_suffix;
    if ( $verbose > 1 ) print $sql_action;
    $res=mysql_query($sql);
    if ( mysql_error() ) {
        if ( $verbose > 1 ) {
	    print "RESULT: ERROR\n$sql" . "\n";
            print mysql_error() . "\n";
	}
	$errors++;
    } else {
        if ( $verbose > 1 ) print "RESULT: OK\n";
	if ( $sql_state=="INSERTED" ) $inserts++;
	if ( $sql_state=="UPDATED" ) $updates++;
    }
    // print "C:" . $fields[100] . "=>" . $fields[104] . "\n";
    // print "C:" . $fields[103] . "\n";

}

$sql="delete from $db_table where reserved_1 = 'OLD' and reserved_2='";
$sql .= $filekey . "'";
if ( $verbose > 0 ) print "Deleting obsolete records: ";
$res=mysql_query($sql);
print mysql_error();

// re-calculate sums
$sql="update pmd_category set fcounter = (select count(*) from $db_table where $db_table.category like concat(pmd_category.selector,'#%#%'))";
$res=mysql_query($sql);

if ( $verbose > 0 ) {
    print mysql_affected_rows() . "\n";
    print "Records updated: $updates\n";
    print "Records inserted: $inserts\n";
    print "Records skipped: $skipped\n";
    print "Errors: $errors\n";
}


function phoneformat($string) {
    if ( strlen($string) != 11 ) return trim($string);
    return trim( 
    sprintf("(%s) %s-%s",substr($string,0,3),substr($string,4,3),substr($string,7,4))
    );
}
function oldphoneformat($string) {
    if ( strlen($string) != 10 ) return trim($string);
    return trim( 
    sprintf("(%s) %s-%s",substr($string,0,3),substr($string,3,3),substr($string,6,4))
    );
}

?>

This has worked fine for years and just started acting up. Can anyone see the problem?

Thank you in advance for your help, I do appreciate it.

Recommended Answers

All 2 Replies

$ha = fopen($filename,"r");
while ( !feof($ha) ) {

You will get the error message if the fopen fails. If you want to avoid these errors, try:

$ha = fopen($filename,"r");
if (! $ha) {
  // could not open stream
}
else 
  while ( !feof($ha) ) {

If it worked before, then something has changed, a php upgrade, a change in the ini settings, firewall or proxy not allowing to connect...

Thanks a million, this looks like it will fix it.
Rain

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.