2 part question for file editing and cron jobs

The last part of a project that i’m working involves uploading files to a designated folder, then using a cron job to look those files, delete parts of the file, upload what’s left to a mysql table, then move the file somewhere else.

Currently i have the upload and import working if i manually edit the csv file to delete what’s needed.

Keep in mind that one of our other developers wrote this code, but is no longer with us. But if there’s another way of doing this, i’m all for it. I modified it to allow for different files to be imported through 1 script.

[php]

<?php require 'config.php'; ?>
	<h2>Upload &amp; Import Sales and/or Trends</h2>
<?php if(isset($_POST['submit_ureport'])) { $companyArr = array('venzo_app_sales', 'venzo_app_trends', 'venzo_itunes_sales', 'venzo_itunes_trends'); $company = mysql_real_escape_string(strtolower($_POST['company'])); if(in_array($company, $companyArr)) { if($company == "venzo_app_sales") { $path = "sales/app/"; } elseif($company == "venzo_app_trends") { $path = "trends/app/"; } elseif($company == "venzo_itunes_sales") { $path = "sales/itunes/"; } elseif($company == "venzo_itunes_trends") { $path = "trends/itunes/"; } $companyTbl = $company; } if($_FILES['file']['error'] > 0) { echo "Return Code: {$_FILES['file']['error']}
"; } else { if(file_exists($path . $_FILES['file']['name'])) { echo "{$_FILES['file']['name']} already exists."; } else { if(move_uploaded_file($_FILES['file']['tmp_name'], $path . $_FILES['file']['name'])) { //echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; $file = $path.$_FILES['file']['name']; $fcontents = file($file, 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($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."
"; if($company == "venzo_itunes_trends" || $company == "venzo_app_trends") { $sqlFieldNum = mysql_num_rows($qry); while($field = mysql_fetch_assoc($qry)) { $sqlFields[] = strtolower($field['Field']); } } else { $sqlFieldNum = mysql_num_rows($qry)-2; while($field = mysql_fetch_assoc($qry)) { if($field['Field'] != 'id' && $field['Field'] != 'report_paid') $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++) { echo "SQL: {$sqlFields[$a]} - Raw report: {$fields[0][$a]} - Underline report: {$fields[1][$a]} - Strip-spaces report: {$fields[2][$a]}
"; if($sqlFields[$a] != $fields[0][$a] && $sqlFields[$a] != $fields[1][$a] && $sqlFields[$a] != $fields[2][$a]) { $error = true; echo "Report fields do not match the table fields for `{$company}`! Field `{$sqlFields[$a]}` should not exist! Estimated expected field name: '" . $fields[0][$a] . "'.

"; } else { unset($tfields[$a]); } } /*check for fields in the report that are not in the DB*/ if(count($tfields) > 0) { $error = true; echo "Report fields do not match the table fields for `{$company}`! Field(s) `" . implode("`, `", $tfields) . "` are missing!

"; } } /*if there is an error, stop here and delete the file*/ if($error == true) { unlink($file); die(); } /*for each record in the report...*/ for($i = 1; $i < count($fcontents); $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) . "')"; mysql_query($sql); if(mysql_errno()) { unlink($file); die(mysql_error()); } } else { echo "Record {$i} not inserted!

"; } } } /*if there is an error, stop here and delete the file*/ if(count($error) > 0) { unlink($file); die(); } if($sql) { $msg = basename($_FILES['file']['name']) . ' has successfully been imported. The information is ready to be displayed
'; } else { $msg = basename($_FILES['file']['name']) . ' has NOT been imported, please try again later!
'; } } } } } ?>
				<br/>
				Please select which report you need to upload:<br /><br />
				<form method="post" action="" enctype="multipart/form-data">
					<label for="company">Mobile or Music? </label>
					<select name="company">
						<optgroup label="Sales">
							<option value="venzo_app_sales">Mobile</option>
							<option value="venzo_itunes_sales">Music</option>
						</optgroup>
						<optgroup label="Trends">
							<option value="venzo_app_trends">Mobile</option>
							<option value="venzo_itunes_trends">Music</option>
						</optgroup>
					</select><br /><br />
					<label for="report">Report: </label><input type="file" name="file" /><br /><br />
				  <div style="text-align:right;">
					<input type="submit" name="submit_ureport" value="Import" style="padding:2px 20px 2px 20px;" />
				  </div>
				</form>
				<br />
				<?=$msg?>[/php]

What i need to know is how to get it automatically look in a folder, open the fie and then delete stuff. This is the first and last line from one of our sales reports.

Start Date End Date UPC ISRC/ISBN Vendor Identifier Quantity Partner Share Extended Partner Share Partner Share Currency Sales or Return Apple Identifier Artist/Show/Developer/Author Title Label/Studio/Network/Developer/Publisher Grid Product Type Identifier ISAN/Other Identifier Country Of Sale Pre-order Flag Promo Code Customer Price Customer Currency 11/27/2011 12/31/2011 USVGG1160301 USVGG1160301 2 1.29 2.58 AUD S 465219812 DJ Ringtones Club Paradise (feat. Drake) [Ringtone] V&G PR AU 2.19 AUD Total_Rows 352 Total_Amount 3696.84 Total_Units 2916

Total_Rows 352
Total_Amount 3696.84
Total_Units 2916

is what i need to delete. i know how to open the file, but not how to delete just those totals.

anyone have any ideas on how to accomplish this?

Ah, all that code to ask to delete three lines? LOL Are these three lines always at the bottom? If so, just delete the last three lines.

Or, just parse for the three titles and skip that part…
So, if the layout is ALWAYS like this:
Total_Rows 352
Total_Amount 3696.84
Total_Units 2916
Load the file into a string and do a search for “Total_Rows” and trim off the rest…
Something like:
$FullTextReportWithoutTotals = strstr($FullTextofReport, ‘Total_Rows’, true);

STRSTR function looks thru a string for a substring and then returns all BEFORE it if the true option is there.

Not sure if that is what you wanted, but, hope it helps…

The parser doesn’t delete anything, that’s just the code to import the manually modified csv file into the db table.

I don’t want to delete everything before that, i need to delete just the totals. Yes, they’re always at the bottom.

I gave you the code line and explained how to do it.

You simply use " $FullTextReportWithoutTotals = strstr($FullTextOfReport, ‘Total_Rows’, true); "

Where your full text of the report is PARSED to locate the Total_Rows line and returns everything BEFORE it.

Your CSV file is just a text file with delimiters. You can read it in as a text file into a variable and then use the line above to strip out the totals. Then, use that file to do the rest of your code. You would do the above line before you do your converting to the database…

If you do not understand how to read a file into a variable, here is sample code to do so…

$ReportFile = "LocationOfReportCSVfile/ReportFileName.csv";
$fh = fopen($ReportFile, 'r');
$FullTextOfReport = fread($fh, filesize($ReportFile));
fclose($fh);

// variable $FullTextOfReport   now contains the entire report text
$FullTextReportWithoutTotals = strstr($FullTextOfReport, 'Total_Rows', true);
// vaiable $FullTextReportWithoutTotals  now contains entire report text WITHOUT totals...

At the end of this code, you can either write it back out to a temp file and use it to go thru your database conversion routines or just use it to read from instead of the live file. I would test both and use whichever runs the fastest.

Hope that cleared it up… Good luck…

Yes, that helped. Ill look at it more when I get home to see how to integrte it and then create the cron job

thanks for the help

alrighty. I need one more tidbit to finish this. Basically, i need this file to scan a directory and if there’s any files in it, determine which db table to use depending on file name, then procede to the import and other stuff (already have this).

The problem seems to lie with the filename. They come in a standard format, which is

trends name format: S_W_account_xxxxxxxx.txt (ex. S_W_12345679_13245.txt)
sales name format: account_xxxx_2digt country code.txt (ex. 13245678_1211_US.txt)

The db table needs to be ablel to change depending on what the first part of the filename is. For trends, it would be S_W_123465 and for sales, it would be 12345678. There’s an account for music (trends and sales) and apps (trends and sales).

From reading through the php functions list, i need to open the directory (admin/files), the scan the files, get the basename (or i guess i could just explode to get it, or use basename()), close the dir and the proceed with the trimming and importing.

Also, would it be easier to import the data using fgetcsv() ? i saw a few functions in the php.net example that would shorten up the code considerbly.

Well, you can get filenames in a folder and then just parse thru them and compare the left side.

if(left($filename, 3)==“S_W”)

But, another way is to use EXPLODE, something like this:

InputData = explode("_", $filename);

so, S_W_123_123 becomes InputData[0]=“S” InputData[1]=“W” etc…

Then, just parse it with a couple IF’s to figure out which it is… Should work…
Off to bed, but post away if you can’t figure it all out. Post the code… Good luck

So it would something like:
[php]

<?php if($handle = opendir('/path/to/files')) { while (false !== ($file = readdir($handle))) { if(left($filename, 12)=="S_W_xxxx") { $path = 'some/path'; $table = 'db table name'; } elseif(......) { // and so on } } } closedir($handle); ?>

[/php]
?

Yes, that is basically it.

Note that you cold place all the accounts into a table and just compare them.

$AccountNumbers[0]=“S_W_00003”;
$AccountNumbers[1]=“S_W_00013”;
$AccountNumbers[2]=“S_W_00023”;

Or, even make it a linked list (multidimensional array) with the second field being the db-table name:

$AccountNumbers[0][0]=“S_W_00003”;
$AccountNumbers[1][0]=“S_W_00013”;
$AccountNumbers[2][0]=“S_W_00023”;
$AccountNumbers[0][1]=“table3”;
$AccountNumbers[1][1]=“table13”;
$AccountNumbers[2][1]=“table23”;

Search the arrays first row for the S_W part and then, you have the DB-table-name… Would save having a ton of if(left(… code in your page. Just a exists-in-array command. (Forgot the format for if exists in array)

Wow, I just thought of an even better way, add a text field to your database. call it ‘DBNameCriteria" or something that make sense. Then, put “S_W_00003” in it for the “account 3”, etc. Then, just do a SQL call to locate the correct account. Something like:
"SELECT AccoutNumber from account_names where DBNameCriteria=’" . left($filename, 12) . “’”
(Note that you can use /" for quotes or use ’ " no spaces for quote and quotes in above.)

That way, you would just do a query and the results would be the correct account number. No code looking up and comparing until you find the correct account number. You would have to map out what you store as the crteria…

Well, a bit for you to think about…

Well, there’s only 4 tables and 2 account numbers, so i don’t think i need to go through the trouble of creating a table for it. This is what i have so far:

[php]
if($handle = opendir(’/home/watunesc/public_html/venzodigital.com/admin/files’)) {
While (false !== ($file = readdir($handle))) {
if(left($file, 12)== “S_W_80045583”) {
$path = ‘/home/watunesc/public_html/venzodigital.com/admin/trends/itunes/’;
$companyTbl = ‘venzo_trends_sales’;
$ReportFile = “/home/watunesc/public_html/venzodigital.com/admin/files/”.$file;
$fh = fopen($ReportFile, ‘r’);
$FullTextOfReport = fread($fh, filesize($ReportFile));fclose($fh);
$FullTextReportWithoutTotals = strstr($FullTextOfReport, ‘Total_Rows’, true);
} elseif(left($file, 8)== “80045583”) {
$path = ‘/home/watunesc/public_html/venzodigital.com/admin/sales/itunes/’;
$companyTbl = ‘venzo_itunes_sales’;
$ReportFile = “/home/watunesc/public_html/venzodigital.com/admin/files/”.$file;
$fh = fopen($ReportFile, ‘r’);
$FullTextOfReport = fread($fh, filesize($ReportFile));fclose($fh);
$FullTextReportWithoutTotals = strstr($FullTextOfReport, ‘Total_Rows’, true);
} elseif(left($file, 12)==“S_W_85409067”) {
$path = ‘/home/watunesc/public_html/venzodigital.com/admin/trends/app/’;
$companyTbl = ‘venzo_app_trends’;
$ReportFile = “/home/watunesc/public_html/venzodigital.com/admin/files/”.$file;
$fh = fopen($ReportFile, ‘r’);
$FullTextOfReport = fread($fh, filesize($ReportFile));fclose($fh);
$FullTextReportWithoutTotals = strstr($FullTextOfReport, ‘Total_Rows’, true);
} elseif(left($file, 8)== “85409067”) {
$path = ‘/home/watunesc/public_html/venzodigital.com/admin/sales/app/’;
$companyTbl = ‘venzo_app_sales’;
$ReportFile = “/home/watunesc/public_html/venzodigital.com/admin/files/”.$file;
$fh = fopen($ReportFile, ‘r’);
$FullTextOfReport = fread($fh, filesize($ReportFile));fclose($fh);
$FullTextReportWithoutTotals = strstr($FullTextOfReport, ‘Total_Rows’, true);
}
}
}
closedir($handle);
[/php]

The only reason i’m using full paths is because the last time i had to do a cron job, it wouldn’t find the files using regular path names.

Looks good. Glad I could help! Let us know how the finished product works out for you.

I am always amazed at what people need to program. Pulling a report for a file, stripping off part, passing on the rest to a certain account file… Very cool this type of thing can be done! Always smile when a project gets solved!

Good luck with the rest of it…

Usually if someone can point me in the right direction and give me some sample code, i can figure out the rest by doing some research and most of the time, i figure it out before i finish typing the help message :slight_smile:

I don’t know if it works yet, as i’ve gotta get to my real job, but here’s what i ended up with so far. I still need to add a log feature so i know if something goes wrong, but i think i can figure that out using some other code i have.

[php]<?php
require ‘/home/watunesc/public_html/venzodigital.com/admin/config.php’

if($handle = opendir(’/home/watunesc/public_html/venzodigital.com/admin/files’)) {
While (false !== ($file = readdir($handle))) {
if(left($file, 12)== “S_W_80045583”) {
$path = ‘/home/watunesc/public_html/venzodigital.com/admin/trends/itunes/’;
$companyTbl = ‘venzo_trends_sales’;
$ReportFile = “/home/watunesc/public_html/venzodigital.com/admin/files/”.$file;
$fh = fopen($ReportFile, ‘r’);
$FullTextOfReport = fread($fh, filesize($ReportFile));fclose($fh);
$FullTextReportWithoutTotals = strstr($FullTextOfReport, ‘Total_Rows’, true);
} elseif(left($file, 8)== “80045583”) {
$path = ‘/home/watunesc/public_html/venzodigital.com/admin/sales/itunes/’;
$companyTbl = ‘venzo_itunes_sales’;
$ReportFile = “/home/watunesc/public_html/venzodigital.com/admin/files/”.$file;
$fh = fopen($ReportFile, ‘r’);
$FullTextOfReport = fread($fh, filesize($ReportFile));fclose($fh);
$FullTextReportWithoutTotals = strstr($FullTextOfReport, ‘Total_Rows’, true);
} elseif(left($file, 12)==“S_W_85409067”) {
$path = ‘/home/watunesc/public_html/venzodigital.com/admin/trends/app/’;
$companyTbl = ‘venzo_app_trends’;
$ReportFile = “/home/watunesc/public_html/venzodigital.com/admin/files/”.$file;
$fh = fopen($ReportFile, ‘r’);
$FullTextOfReport = fread($fh, filesize($ReportFile));fclose($fh);
$FullTextReportWithoutTotals = strstr($FullTextOfReport, ‘Total_Rows’, true);
} elseif(left($file, 8)== “85409067”) {
$path = ‘/home/watunesc/public_html/venzodigital.com/admin/sales/app/’;
$companyTbl = ‘venzo_app_sales’;
$ReportFile = “/home/watunesc/public_html/venzodigital.com/admin/files/”.$file;
$fh = fopen($ReportFile, ‘r’);
$FullTextOfReport = fread($fh, filesize($ReportFile));fclose($fh);
$FullTextReportWithoutTotals = strstr($FullTextOfReport, ‘Total_Rows’, true);
}
}
}
closedir($handle);

$fcontents = file($path.$ReportFile, 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($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.”
”;
if($company == “venzo_itunes_trends” || $company == “venzo_app_trends”) {
$sqlFieldNum = mysql_num_rows($qry);
while($field = mysql_fetch_assoc($qry)) {
$sqlFields[] = strtolower($field[‘Field’]);
}
} else {
$sqlFieldNum = mysql_num_rows($qry)-2;
while($field = mysql_fetch_assoc($qry)) {
if($field[‘Field’] != ‘id’ && $field[‘Field’] != ‘report_paid’)
$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($file);
	die();
}
    
/*for each record in the report...*/
for($i = 1; $i < count($fcontents); $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]}<br />";
			}
			echo "<b># of fields in the table (" . $sqlFieldNum . ") is greater than the # of fields in the report (" . count($arr) . ")!</b><br /><br />";
          
			/*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]}<br />";
			}
			echo "<b># of fields in the report (" . count($arr) . ") is greater than the # of fields in the table (" . $sqlFieldNum . ")!</b><br /><br />";
		}
        
		/*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) . "')";
			mysql_query($sql);
			if(mysql_errno()) {
				unlink($file);
				die(mysql_error());
			}
		} else {
			echo "<b>Record {$i} not inserted!</b><br /><br />";
		}
	}
}
/*if there is an error, stop here and delete the file*/
if(count($error) > 0) {
	unlink($file);
	die();
}
 
if($sql) {
	$msg =  basename($_FILES['file']['name']) . ' has successfully been imported. The information is ready to be displayed<br />';
} else {
	$msg =  basename($_FILES['file']['name']) . ' has NOT been imported, please try again later!<br />';
}

}
?> [/php]

Looks nice! I see you did extensive error checking. I seldom see that these days. Very good! One thing, if you add an $errorcode to each area where you catch an error, you can do a die($errorcode) instead of just die()… This is very helpful in sorting out errors.
I use something like: $errorcode=“Error: Missing Field (From ‘check fields missing in report’)”;
Or something very clear as what the error is and where in the program it occurred. This will save you a lot of time wondering which DIE() created this error…

Once this is tested, let us know how it worked out. I will close this thread when it works as you want it to…

There were some messages for those errors, but because this is being converted to a cron job, i have to change them so they output to a log instead, which i just thought of a way to do it while typing this :slight_smile: seems to happen to me a lot.

I really do appreciate the help, if i could figure out how to give rep, i would :wink:

This site accepts (+) or (-) Karma! It’s a click on the left side if you want to add a little thanks.

It isn’t used much on this site, but, it is there… (Not asking for it, I love to help!!!)

I see where it says Karma: +3/-0, but its not a link or clickable.

You click on it when you are reading the persons note that gave you info…

I think it explains in the FAQ at the top under general or something…

Well, it almost worked. Took quite a bit of debugging. it does work as it is now, but how it should. Right now, it loops through 1 file every few minutes (which is what the cron job is set up as). But, it should be doing one file after another, not one every 5 minutes.

This is the working code:
[php]

<?php mysql_connect('', '', '') or die(mysql_error()); // values removed for obvious reasons lol mysql_select_db(') or die(mysql_error()); function left($string, $count){ return substr($string, 0, $count); } $oops = array(); if($handle = opendir('/home/watunesc/public_html/venzodigital.com/admin/files')) { While (false !== ($file = readdir($handle))) { $filen = basename($file); if(left($filen, 12) == "S_W_80045583") { $path = '/home/watunesc/public_html/venzodigital.com/admin/trends/itunes'; $companyTbl = 'venzo_trends_sales'; $ReportFile = "/home/watunesc/public_html/venzodigital.com/admin/files/".$file; $fh = fopen($ReportFile, 'r'); $FullTextOfReport = fread($fh, filesize($ReportFile)); fclose($fh); $FullTextReportWithoutTotals = substr($FullTextOfReport, 0, strpos($FullTextOfReport, 'Total_Rows')); copy($ReportFile, $path."/".$file); } elseif(left($filen, 8) == "80045583") { // what i'm working with $path = '/home/watunesc/public_html/venzodigital.com/admin/sales/itunes/'; $companyTbl = 'venzo_itunes_sales'; $ReportFile = '/home/watunesc/public_html/venzodigital.com/admin/files/'.$file; $fh = fopen($ReportFile, 'r'); $FullTextOfReport = fread($fh, filesize($ReportFile)); fclose($fh); $FullTextReportWithoutTotals = substr($FullTextOfReport, 0, strpos($FullTextOfReport, 'Total_Rows')); copy($ReportFile, $path."/".$file); } elseif(left($filen, 12) =="S_W_85409067") { $path = '/home/watunesc/public_html/venzodigital.com/admin/trends/app/'; $companyTbl = 'venzo_app_trends'; $ReportFile = "/home/watunesc/public_html/venzodigital.com/admin/files/".$file; $fh = fopen($ReportFile, 'r'); $FullTextOfReport = fread($fh, filesize($ReportFile)); fclose($fh); $FullTextReportWithoutTotals = substr($FullTextOfReport, 0, strpos($FullTextOfReport, 'Total_Rows')); copy($ReportFile, $path."/".$file); } elseif(left($filen, 8) == "85409067") { $path = '/home/watunesc/public_html/venzodigital.com/admin/sales/app/'; $companyTbl = 'venzo_app_sales'; $ReportFile = "/home/watunesc/public_html/venzodigital.com/admin/files/".$file; $fh = fopen($ReportFile, 'r'); $FullTextOfReport = fread($fh, filesize($ReportFile)); fclose($fh); $FullTextReportWithoutTotals = substr($FullTextOfReport, 0, strpos($FullTextOfReport, 'Total_Rows')); copy($ReportFile, $path."/".$file); } if(empty($file)) { $oops[8] = "No files present to upload"; } } } closedir($handle); $fcontents = file($ReportFile, 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($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."
"; if($companyTbl == "venzo_itunes_trends" || $companyTbl == "venzo_app_trends") { $sqlFieldNum = mysql_num_rows($qry); while($field = mysql_fetch_assoc($qry)) { $sqlFields[] = strtolower($field['Field']); } } else { $sqlFieldNum = mysql_num_rows($qry)-2; while($field = mysql_fetch_assoc($qry)) { if($field['Field'] != 'id' && $field['Field'] != 'report_paid') { $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($file); die(); } /*for each record in the report...*/ for($i = 1; $i < count($fcontents); $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) { $sql = "INSERT INTO {$companyTbl}(" . implode(', ', $sqlFields) . ") VALUES ('" . implode("', '", $arr) . "')"; mysql_query($sql); if(mysql_errno()) { unlink($file); die(mysql_error()); } } else { $oops[5] = "Record {$i} not inserted!"; } } } /*if there is an error, stop here and delete the file*/ if(count($error) > 0) { unlink($ReportFile); die(); } if($sql) { $oops[6] = basename($_FILES['file']['name']) . ' has successfully been imported. The information is ready to be displayed'; } else { $oops[7] = basename($_FILES['file']['name']) . ' has NOT been imported, please try again later!'; } $log = "/home/watunesc/public_html/venzodigital.com/admin/import_".date('m/d/Y'); $open = fopen($log, 'w'); for($p = 0; $p < count($oops); $p++) { $data = $oops."\n"; } fwrite($open, $data); fclose($open); } ?>[/php]

Its also worth noting that the way the strstr() was written is only valid if you’re using php 5.3 and above. i was able to find a work around for previous versions. also that left() is not a built in function for php, its an ASP convert.

Anyways, i can’t test any new code until more sales files are released, but in the mean time, do i just need to move everything inside the while loop or will that screw things up?

Sorry Sorry Sorry, I have an “Include” that setups up the “Left” function, almost exactly like yours. I forgot to mention that. I do not use ASP anymore since PHP has taken over… Hope that did not cause you any big delays!

As far as looping thru the files, I think it is your IF for the loop thru all files. The following code is a sample of how you usually loop thru a directory (folder) of files. Maybe this will help…

Check all files…
[php]
//Directory or Folder to check
$foldername = “…/datafolder/”; //you could use full folder such as www.mysite.com/blah/blah/ etc…

//Locate all files with a .doc type for use (Just a sample, you can use . for all files)
$filenames = glob($foldername . “*.doc”);

//Access each file (Loop thru all filenames getting on filename at a time)
foreach($filenames as $filename)
{
echo $filename; //Do something with the filename…
}
[/php]

Hope that helps… And, again sorry about the Left()… :-[

left() wasn’t an issue, i just typed left() into the address bar and got my answer :slight_smile:

So instead of the if statement, just change it to a foreach loops?

Sponsor our Newsletter | Privacy Policy | Terms of Service