ok, I have this script that runs off of a cronjob. it’s supposed to grab a list of files, figure out what goes where and then import it into the given db table. it does work, to an extent. I don’t know why, but it keeps saying the file was unlinked when its sitting right there in the folder. below is the script.
[php]<?php
ini_set(“magic_quotes_gpc”, 0);
mysql_connect(‘localhost’, ‘xxxx’, ‘xxxx’) or die(mysql_error());
mysql_select_db(‘venzo_primary’) or die(mysql_error());
define(‘APP_PATH’, ‘/home/venzo/public_html/admin/’);
function left($string, $count){
return substr($string, 0, $count);
}
//$file_list=;
foreach(glob(APP_PATH.“files/*.txt”) as $file) {
$filen = basename($file);
if(left($filen, 12) == "S_W_80045583") {
$path = APP_PATH.'trends/itunes';
$companyTbl = 'venzo_itunes_trends';
$ReportFile = APP_PATH."files/".$filen;
copy($ReportFile, $path."/".$filen);
$process = 1;
} elseif(left($filen, 12) == "S_D_80045583") {
$path = APP_PATH.'trends/itunes';
$companyTbl = 'venzo_itunes_dtrends';
$ReportFile = APP_PATH."files/".$filen;
copy($ReportFile, $path."/".$filen);
$process = 1;
} elseif(left($filen, 12) == "S_W_85409067") {
$path = APP_PATH.'trends/app';
$companyTbl = 'venzo_app_trends';
$ReportFile = APP_PATH."files/".$filen;
copy($ReportFile, $path."/".$filen);
$process = 1;
} elseif(left($filen, 12) == "S_D_85409067") {
$path = APP_PATH.'trends/app';
$companyTbl = 'venzo_app_dtrends';
$ReportFile = APP_PATH."files/".$filen;
copy($ReportFile, $path."/".$filen);
$process = 1;
}
//echo $ReportFile."<br />";
if($process == 1) {
$fcontents = file($ReportFile, FILE_IGNORE_NEW_LINES); //line 44
/*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());
//echo $company."<br />";
$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); //line 88
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++) {
$oops[0] = "{$a}) MYSQL: {$a} => {$sqlFields[$a]} - Report: {$a} => {$arr[$a]}";
}
$oops[1] = "# 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++) {
$oops[3] = "{$a}) MYSQL: {$a} => {$sqlFields[$a]} - Report: {$a} => {$arr[$a]}";
}
$oops[4] = "# 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 && !$oops) {
$sql = "INSERT INTO {$companyTbl}(" . implode(', ', $sqlFields) . ") VALUES ('" . implode("', '", $arr) . "')";
$ins = mysql_query($sql);
if($ins) {
unlink($file); //line 126
} else {
echo "Problem with $filen";
}
} else {
$oops[5] = "Record {$i} not inserted!";
foreach($oops AS $mes) {
echo $mes."<br />";
}
}
}
}
unset($sqlFields);
unset($fcontents);
unset($line);
/*if there is an error, stop here and delete the file*/
if(count($error) > 0) {
unlink($path."/".$file);
die();
}
}
}
?>[/php] and these are the errors i’m getting from the cronjob output.
The error messages were truncated, the whole message is several lines long. I put the relevant line numbers in a comment so you'll know what's what.
Warning: unlink(/home/venzo/public_html/admin/files/S_W_85409067_20130609.txt): No such file or directory in /home/venzo/public_html/admin/import_files_trends.php on line 126
Warning: unlink(/home/venzo/public_html/admin/files/S_W_85409067_20130609.txt): No such file or directory in /home/venzo/public_html/admin/import_files_trends.php on line 126
Warning: unlink(/home/venzo/public_html/admin/files/S_W_85409067_20130609.txt): No such file or directory in /home/venzo/public_html/admin/import_files_trends.php on line 126
Warning: unlink(/home/venzo/public_html/admin/files/S_W_85409067_20130609.txt): No such file or directory in /home/venzo/public_html/admin/import_files_trends.php on line 126
Warning: unlink(/home/venzo/public_html/admin/files/S_W_85409067_20130609.txt): No such file or directory in /home/venzo/public_html/admin/import_files_trends.php on line 126
Warning: unlink(/home/venzo/public_html/admin/files/S_W_85409067_20130609.txt): No such file or directory in /home/venzo/public_html/admin/import_files_trends.php on line 126
Warning: file(/home/venzo/public_html/admin/files/S_W_85409067_20130609.txt): failed to open stream: No such file or directory in /home/venzo/public_html/admin/import_files_trends.php on line 44
Warning: unlink(/home/venzo/public_html/admin/trends/app/spotify-track-for-venzodigital-20134.txt): No such file or directory in /home/venzo/public_html/admin/import_files_trends.php on line 88