Jump to content

PHP file handling not working


rossdagley

Recommended Posts

Hi guys. I really hope you can help me - I've some experience with PHP but not much, so struggling to debug whats happening. Perhaps you guys can assist!

 

I have an old PHP script written by another site that unfortunately no longer supported. I'm trying to get it to simply pull in data from a CSV file and import it into my mysql database. For some reason, the code isn't working correctly, and I get an error.

 

My setup is PHP 5 on Windows 2003.

 

here's the code:

#!/usr/bin/php -q
<?php

require 'web/mysql_config.php';
require 'include/mysql_connect.php';
require 'include/func.php';
require 'include/ftp_fetch_class.php';

$sql = "SELECT * FROM {$dbtable_prefix}config";
$result = query($sql);
while ($data = mysql_fetch_assoc($result)) $config[$data['name']] = $data['value'];

echo "DCLP {$config['version']} starting @ ", date($config['datetime_format']), "\n";

$sql = "SELECT * FROM {$dbtable_prefix}server WHERE parse = 1";
$result = query($sql);
$active_server_count = mysql_num_rows($result);

if ($active_server_count) {
while ($data = mysql_fetch_assoc($result)) $server[] = $data;
} else {
exit("Exiting: There are no servers setup for parsing.\n\n");
}

foreach ($server as $s) {

$parse = FALSE;

echo "\nProcessing log for server: {$s['name']} ...\n";

// FTP active block
if ($s['ftp']) {

	echo "\tUsing FTP ...\n";

	// check if the ftp object exists
	if (!$ftp) {
		$ftp = new dclp_ftp_fetch;
	}

	$ftp->config($s['ftp_server'], $s['ftp_port'], $s['ftp_user'], $s['ftp_pass'], $s['log_path']);

	if (!$ftp->go()) {
		echo "\t{$ftp->error}\n";
		$parse = FALSE;
	} else {
		$parse = TRUE;
	}

// Local parsing block
} else {

	$filename = 'adminlog.csv';
	$backup_filename = 'AdminLog.NEW';
	$file_rename = 'adminlog.dclp';

	if (!is_file("{$s['log_path']}/$filename")) {
		echo "\t$filename not found. Trying $backup_filename...\n";
		$filename = $backup_filename;
		if (!is_file($filename)) {
			echo "\t$filename not found. There might not have been any new kicks/bans since DCLP last ran.\n";
			$parse = FALSE;
		} else {
			$parse = TRUE;
		}
	} else {
		$parse = TRUE;
	}

	if ($parse) {
		if (!rename($filename, $file_rename)) {
			echo "\tCould not rename $filename to $file_rename. Skipping this server.\n";
			$parse = FALSE;
		}
	}
}

if ($parse) {
	$count = 0;
	$fp = fopen("adminlog.dclp", "r");

	while (FALSE !== ($array = fgetcsv($fp,1024))) {

		if ($array != "" && !@preg_match("/address/", $array[1])) {
			$playername = mysql_escape_string($array[0]);
			$admin = mysql_escape_string($array[4]);
			$map = mysql_escape_string($array[6]);
			$reason = mysql_escape_string($array[9]);

			if ($array[2] != "[unknown]") {
				$player_ip = "'" . sprintf("%u", ip2long($array[1])) . "'";
			} else {
				$player_ip = "NULL";
			}

			if ($array[8] == 0) {
				$expires = "NULL";
			} else {
				$expires = "'" . ($array[8] - $array[5]) . "'";
			}

			$sql = "INSERT INTO {$dbtable_prefix}event SET
				playername = '$playername',
				player_ip = $player_ip,
				keyhash = '$array[2]',
				guid = '$array[3]',
				admin = '$admin',
				timestamp = $array[5],
				map = '$map',
				action = '$array[7]',
				expires = $expires,
				reason = '$reason',
				server_id = {$s['id']}";
			$result = query($sql);
			$count++;

			unset($array);
			unset($playername);
			unset($admin);
			unset($map);
			unset($reason);
			unset($expires);
			unset($player_ip);
		}
	}
	echo "\t$count kicks/bans entered into database.\n";
	fclose($fp);
}
}

?>

 

After running this, it always errors at:

 

Connection to MySQL server successful.
Using pandpkicks as database.
DCLP .40 starting @ 2009-11-12 @ 12:03PM

Processing log for server: Old School ...
Could not rename adminlog.csv to adminlog.dclp. Skipping this server.

 

If I remove (or rename obviously) the actual file i'm working with (adminlog.csv) then it throws up the error that the file cannot be found - so it's clearly SEEING the file - just not finishing running my script...

 

I really hope you guys can see something obvious, or explain something I can check?

 

Thanks!

 

Ross

Link to comment
https://forums.phpfreaks.com/topic/181232-php-file-handling-not-working/
Share on other sites

  • 2 weeks later...

Cant edit the previous post, so here's what I added:

 

Thanks guys. I've got a workaround thanks to the debug code posted above. Not a fix, but it gave me clues so I can see whats going on ;)

 

I now get this (I've modified the php code a little to show me the path it's trying to use:

Processing log for server: Old School ...
PHP Warning:  rename(adminlog.csv,adminlog.dclp): No such file or directory in C:\games\kicksandbans\dclp.php on line 75


Warning: rename(adminlog.csv,adminlog.dclp): No such file or directory in C:\games\kicksandbans\dclp.php on line 75
        Could not rename c:\games\DC\adminlog.csv to adminlog.dclp. Skipping this server.

 

It's pulling this path out of a mysql database - I've tried all combinations of forward and back slashes, in case this is the issue, but the file is there in the path i'm showing.

 

Any pointers greatly appreciated!

 

If I copy the csv file into the root directory where the php script is running, it works fine.  Unfortunately, as much as that is a workaround, thats not a solution I can live with.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.