ok, I've been at this for months and each time I think I get it fixed, it ends up broken again. i'm tired of looking at it, thinking I know what's wrong when I really don't have a clue. This script was originally made to import files that were manually uploaded, then it was changed to handle multiple files in a designated folder, running from a cronjob. I know it needs to be in a loop, but what I have isn't working and its giving me fits with all the error messages:
Notice: Undefined variable: error in /home/venzo/public_html/admin/import_files_trends.php on line 121
Notice: Undefined variable: error in /home/venzo/public_html/admin/import_files_trends.php on line 156
S_D_80045583_20130616.txt has been imported
Notice: Undefined variable: error in /home/venzo/public_html/admin/import_files_trends.php on line 156
S_D_80045583_20130616.txt has been imported
Warning: copy(/home/venzo/public_html/admin/files/S_D_80045583_20130616.txt): failed to open stream: No such file or directory in /home/venzo/public_html/admin/import_files_trends.php on line 161
Warning: unlink(/home/venzo/public_html/admin/files/S_D_80045583_20130616.txt): No such file or directory in /home/venzo/public_html/admin/import_files_trends.php on line 162
Notice: Undefined variable: error in /home/venzo/public_html/admin/import_files_trends.php on line 156
S_D_80045583_20130616.txt has been imported
Warning: copy(/home/venzo/public_html/admin/files/S_D_80045583_20130616.txt): failed to open stream: No such file or directory in /home/venzo/public_html/admin/import_files_trends.php on line 161
Warning: unlink(/home/venzo/public_html/admin/files/S_D_80045583_20130616.txt): No such file or directory in /home/venzo/public_html/admin/import_files_trends.php on line 162
Notice: Undefined variable: error in /home/venzo/public_html/admin/import_files_trends.php on line 156
S_D_80045583_20130616.txt has been imported
Warning: copy(/home/venzo/public_html/admin/files/S_D_80045583_20130616.txt): failed to open stream: No such file or directory in /home/venzo/public_html/admin/import_files_trends.php on line 161
Warning: unlink(/home/venzo/public_html/admin/files/S_D_80045583_20130616.txt): No such file or directory in /home/venzo/public_html/admin/import_files_trends.php on line 162
Notice: Undefined variable: error in /home/venzo/public_html/admin/import_files_trends.php on line 156
S_D_80045583_20130616.txt has been imported
Warning: copy(/home/venzo/public_html/admin/files/S_D_80045583_20130616.txt): failed to open stream: No such file or directory in /home/venzo/public_html/admin/import_files_trends.php on line 161
Warning: unlink(/home/venzo/public_html/admin/files/S_D_80045583_20130616.txt): No such file or directory in /home/venzo/public_html/admin/import_files_trends.php on line 162
Notice: Undefined variable: error in /home/venzo/public_html/admin/import_files_trends.php on line 156
S_D_80045583_20130616.txt has been imported
Warning: copy(/home/venzo/public_html/admin/files/S_D_80045583_20130616.txt): failed to open stream: No such file or directory in /home/venzo/public_html/admin/import_files_trends.php on line 161
Warning: unlink(/home/venzo/public_html/admin/files/S_D_80045583_20130616.txt): No such file or directory in /home/venzo/public_html/admin/import_files_trends.php on line 162
Notice: Undefined variable: error in /home/venzo/public_html/admin/import_files_trends.php on line 156
S_D_80045583_20130616.txt has been imported
Lots of those same errors.
I'm sure there's probably a much simpler way of doing this, but at this point, i'd just be happy if it worked as it should. The files are tab delimited txt files. Its important to me to have the error reporting since apple has been randomly changing the file formats on me, adding columns, removing columns, adding rows at the end, all without prior notice.
So here's what I have:
<?php
ini_set("magic_quotes_gpc", 0);
error_reporting(-1);
mysql_connect('xxxx', 'xxxx', 'xxxx') or die(mysql_error());
mysql_select_db('test') or die(mysql_error());define('APP_PATH', '/home/venzo/public_html/admin/');
define('APP', '/home/venzo/public_html/admin/trends/app');
define('MUSIC', '/home/venzo/public_html/admin/trends/itunes');function left($string, $count){
return substr($string, 0, $count);
}$ReportFile = array();
$tables = array();foreach(glob(APP_PATH."files/*.txt") as $file) {
$filen = basename($file);
if(left($filen, 12) == "S_W_80045583") {
$path = APP_PATH.'trends/itunes';
$tables[] = 'venzo_itunes_trends';
$ReportFile[] = APP_PATH."files/".$filen;
if(is_file(MUSIC."/$filen")) {
$process = 0;
}
$process = 1;
} elseif(left($filen, 12) == "S_D_80045583") {
$path = APP_PATH.'trends/itunes';
$tables[] = 'venzo_itunes_dtrends';
$ReportFile[] = APP_PATH."files/".$filen;
if(is_file(MUSIC."/$filen")) {
$process = 0;
}
$process = 1;
} elseif(left($filen, 12) == "S_W_85409067") {
$path = APP_PATH.'trends/app';
$tables[] = 'venzo_app_trends';
$ReportFile[] = APP_PATH."files/".$filen;
if(is_file(APP."/$filen")) {
$process = 0;
}
$process = 1;
} elseif(left($filen, 12) == "S_D_85409067") {
$path = APP_PATH.'trends/app';
$tables[] = 'venzo_app_dtrends';
$ReportFile[] = APP_PATH."files/".$filen;
if(is_file(APP."/$filen")) {
$process = 0;
}
$process = 1;
}
}
if($process == 1) {
foreach ($tables as $companyTbl) {
foreach($ReportFile as $report) {
echo "<pre>";
print_r($tables);
print_r($ReportFile);
echo "</pre>";$fcontents = file($report, FILE_IGNORE_NEW_LINES);
/*check the table exists*/
$qry = current(mysql_fetch_row(mysql_query("SELECT DATABASE()")));
$qry = mysql_query("SHOW TABLES FROM `{$qry}` LIKE '{$companyTbl}'") or die(mysql_error());
if(mysql_num_rows($qry) < 1) {
unlink($path."/".$file);
die("Table `{$companyTbl}` for '{$company}' does not exist!");
}
/*pull the field list out of the DB*/
$qry = mysql_query("SHOW FIELDS FROM `{$companyTbl}`") or die(mysql_error());$sqlFieldNum = mysql_num_rows($qry);
while($field = mysql_fetch_assoc($qry)) {
$sqlFields[] = strtolower($field['Field']);
}
/*check the field list in the report and the field list in the database match*/
if($sqlFieldNum != count($fieldArr = explode("\t", $fcontents[0]))) {
foreach($fieldArr as $key => $value) {
$fields[0][$key] = trim(str_replace('/', '_', strtolower($value)));
$fields[1][$key] = trim(str_replace(array('/', ' '), '_', strtolower($value)));
$fields[2][$key] = trim(str_replace(array('/', ' '), array('_', ''), strtolower($value)));
}
$tfields = $fields[0];
/*check for fields in the DB that are not in the report*/
for($a = 0; $a < count($sqlFields); $a++) {
if($sqlFields[$a] != $fields[0][$a] && $sqlFields[$a] != $fields[1][$a] && $sqlFields[$a] != $fields[2][$a]) {
$error = true;
} else {
unset($tfields[$a]);
}
}
/*check for fields in the report that are not in the DB*/
if(count($tfields) > 0) {
$error = true;
}
}
/*if there is an error, stop here and delete the file*/
if($error == true) {
//unlink($path."/".$filen);
die();
}
/*for each record in the report...*/
for($i = 1; $i < count($fcontents)-3; $i++) {
$line = $fcontents[$i];
$arr = explode("\t", $line);
/*santitise the values*/
foreach($arr as $key => $value) {
$arr[$key] = trim(mysql_real_escape_string($value));
}
if(strtolower($arr[0]) != "total") {
/*number of fields in the DB exceeds number of fields in the record*/
if($sqlFieldNum > count($arr)) {
$error[$i] = true;
for($a = 0; $a < count($sqlFields); $a++) {
echo "{$a}) MYSQL: {$a} => {$sqlFields[$a]} - Report: {$a} => {$arr[$a]}";
}
echo "# of fields in the table (" . $sqlFieldNum . ") is greater than the # of fields in the report (" . count($arr) . ")!";
/*number of fields in the record exceeds number of fields in the DB*/
} else if($sqlFieldNum < count($arr)) {
$error[$i] = true;
for($a = 0; $a < count($arr); $a++) {
echo "{$a}) MYSQL: {$a} => {$sqlFields[$a]} - Report: {$a} => {$arr[$a]}";
}
echo "# of fields in the report (" . count($arr) . ") is greater than the # of fields in the table (" . $sqlFieldNum . ")!";
}
/*if there is no error insert the record into the table else continue onto the next record*/
if($error[$i] != true) {
$sql = "INSERT INTO {$companyTbl}(" . implode(', ', $sqlFields) . ") VALUES ('" . implode("', '", $arr) . "')";
$ins = mysql_query($sql);
echo "$filen has been imported<br>";
if($ins) {
copy($report, $path."/".$filen);
unlink($file);
} else {
echo "Problem with $filen<br>";
}
} else {
echo "Record {$i} not inserted!";
}
}
}unset($sqlFields);
unset($fcontents);
unset($line);
//unset($companyTbl);
//unset($report);/*if there is an error, stop here and delete the file*/
if(count($error) > 0) {
unlink($path."/".$file);
die();
}
}
} //should end the foreach loop
}
?>
I'm not beneath begging, I wouldn't be here if I didn't need the help since I like to troubleshoot these scripts on my own, but i'm completely stumped and have another huge project to work on. I thank you in advance for any direction!