Help. Mad problem!
| From: | Rhys Campbell | Date: | Mon, 17 May 2004 10:09:44 +0000 |
| Subject: | Help. Mad problem! | ||
| Groups: | php.gtk.general | ||
| Request: | Send a blank email to php-gtk-general+get-4497@lists.php.net to get a copy of this message | ||
Got a really odd problems. This script reads text files and inserts names into an Access database.
For some reason it always misses 1 of the files if there are several files. Also if there is only 1
file it doesn't insert the data into the database but seems to think it does. Beenstaring at
this for a while now and can't get it. Any help appreciated.
[code]
<?php
/////////////////////////////////////////////////
// This script scans the contents of the nx //
// inbox for .u files, indicating a user has //
// logged in. The logon names are extracted //
// from the filenames and recorded into an //
// access database. The .u files used are then //
// deleted so they are not later duplicated in //
// the database. //
/////////////////////////////////////////////////
require('odbc.inc'); // include file for db connectivity
require('config.inc'); // include file for nx directory config
if ($handle = opendir($nx_inbox)) // set path for nx inbox
{
/////////////////////////////////////////////////////////////
// //
// Identify .u files //
// //
/////////////////////////////////////////////////////////////
while (false !== ($file = readdir($handle))) // readdir gets next
{
if($extension = substr(strrchr($file, "."), 1 )== 'U') // get file
extension
{ // only works if 1 char
// filename stored
$array_of_filenames[] = $file; // populate the array with filenames
}
}
foreach($array_of_filenames as $key=>$value)
{
echo "$value\n";
}
//////////////////////////////////////////////////////////
// Error handling. Script will exit here if no .U files //
// are present in the inbox. "Invalid argument" errors //
// will occur if we try to so things with non existent //
// files. //
//////////////////////////////////////////////////////////
if(is_null($array_of_filenames[0])) // if first in index is null
{// errors will occur if no .u files are found so...
echo "Error, no .u files found. Exiting!!!!";
exit(0); // terminate script here if no .u files found
}
////////////////////////////////////////////
// //
// extract login names //
// //
////////////////////////////////////////////
foreach($array_of_filenames as $key=>$value)
{
$pos = strrpos($array_of_filenames[$key] = $value, "_");
$array_of_logons[] = substr
($array_of_filenames[$key] = $value, 0, $pos); // returns logon name
}
////////////////////////////////////////////
// //
// Enter logged in users to the database //
// //
////////////////////////////////////////////
foreach($array_of_logons as $key=>$value)
{
odbc_exec($odbc, "INSERT INTO users (logged_in) VALUES ('$value')")
or die (odbc_errormsg()); // insert logged on users into the db
}
////////////////////////////////////////////
// //
// Delete .u files used //
// //
////////////////////////////////////////////
foreach($array_of_filenames as $key=>$value)
{
unlink($nx_inbox.$value); // delete .u files used
}
closedir(); // close the directory when finished
}
?>
[/code]
Regards,
Rhys Campbell
DA Systems
e: rhys.campbell@da-systems.co.uk
t: +44(0)1494 512626
f: +44(0)1494 512727
w: www.da-systems.co.uk