jakebur01 Posted June 9, 2011 Share Posted June 9, 2011 I have a program that is storing failed, returned messages into a text file. I would like PHP to look at each address and handle it, then trunicate the text file, in other words wipe it clean. Here is an example of how it is storing the addresses into the text file: test1d8@thisistheemailtest.com test23@thissithemailte.com test2@testduck.com test3@testduck.com Quote Link to comment https://forums.phpfreaks.com/topic/238903-read-text-file/ Share on other sites More sharing options...
jakebur01 Posted June 9, 2011 Author Share Posted June 9, 2011 I found this: $myFile = "testFile.txt"; $fh = fopen($myFile, 'r'); $theData = fgets($fh); fclose($fh); echo $theData; But, I am not sure that this will loop through each line?? Quote Link to comment https://forums.phpfreaks.com/topic/238903-read-text-file/#findComment-1227578 Share on other sites More sharing options...
wildteen88 Posted June 9, 2011 Share Posted June 9, 2011 I would like PHP to look at each address and handle it, What does that mean? What are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/238903-read-text-file/#findComment-1227579 Share on other sites More sharing options...
WebStyles Posted June 9, 2011 Share Posted June 9, 2011 $file = get_file_contents('filepath/filename.txt'); $lines = explode("\n",$file); // now you have an array with all the lines in it. You can loop through it and do whatever you want. foreach($lines as $line){ ... } // erase file: get_put_contents('filepath/filename.txt',''); Quote Link to comment https://forums.phpfreaks.com/topic/238903-read-text-file/#findComment-1227582 Share on other sites More sharing options...
fugix Posted June 9, 2011 Share Posted June 9, 2011 for starters, to read the text file into a string, use file_get_contents(), as far as the rest is concerned, i agree with wildteen we will need to know exaclty what your you mean by "handle it" Quote Link to comment https://forums.phpfreaks.com/topic/238903-read-text-file/#findComment-1227583 Share on other sites More sharing options...
fugix Posted June 9, 2011 Share Posted June 9, 2011 $file = get_file_contents('filepath/filename.txt'); $lines = explode("\n",$file); // now you have an array with all the lines in it. You can loop through it and do whatever you want. foreach($lines as $line){ ... } // erase file: get_put_contents('filepath/filename.txt',''); get_file_contents() is not a valid function Quote Link to comment https://forums.phpfreaks.com/topic/238903-read-text-file/#findComment-1227584 Share on other sites More sharing options...
jakebur01 Posted June 9, 2011 Author Share Posted June 9, 2011 I am going to use odbc to look at the inventory file and grab the Name, Account #, and Telephone number associated with the returned address. Then, insert it all into a MySQL database. Then, every evening I am going to schedule a php script that will run and look at the table every night. If there is any data in it, I will have it e-mail one of the ladies in the office then trunicate the table. If there is not any data in the table, it will do nothing. Quote Link to comment https://forums.phpfreaks.com/topic/238903-read-text-file/#findComment-1227585 Share on other sites More sharing options...
WebStyles Posted June 9, 2011 Share Posted June 9, 2011 sorry, I meant file_get_contents(); Quote Link to comment https://forums.phpfreaks.com/topic/238903-read-text-file/#findComment-1227587 Share on other sites More sharing options...
jakebur01 Posted June 9, 2011 Author Share Posted June 9, 2011 Thanks. It works good all except for trunicating the file. Fatal error: Call to undefined function get_put_contents() in C:\Inetpub\wwwroot\smithsadvantage\failed_messages.php on line 39 Quote Link to comment https://forums.phpfreaks.com/topic/238903-read-text-file/#findComment-1227598 Share on other sites More sharing options...
jakebur01 Posted June 9, 2011 Author Share Posted June 9, 2011 What about something like this? $myFile = "failed_messages/failed_messages.txt"; $fh = fopen($myFile, 'w'); fclose($fh); Quote Link to comment https://forums.phpfreaks.com/topic/238903-read-text-file/#findComment-1227599 Share on other sites More sharing options...
fugix Posted June 9, 2011 Share Posted June 9, 2011 Thanks. It works good all except for trunicating the file. Fatal error: Call to undefined function get_put_contents() in C:\Inetpub\wwwroot\smithsadvantage\failed_messages.php on line 39 its file_put_contents() Quote Link to comment https://forums.phpfreaks.com/topic/238903-read-text-file/#findComment-1227600 Share on other sites More sharing options...
WebStyles Posted June 9, 2011 Share Posted June 9, 2011 file_put_contents(); // sorry, another typo. I'm kind of dyslexic today. Quote Link to comment https://forums.phpfreaks.com/topic/238903-read-text-file/#findComment-1227601 Share on other sites More sharing options...
jakebur01 Posted June 9, 2011 Author Share Posted June 9, 2011 Cool. Thanks. Here is the full code: <?php set_time_limit(900); ini_set('max_execution_time', '999'); include_once('inc/data.inc'); $file = file_get_contents('failed_messages/failed_messages.txt'); $lines = explode("\n",$file); // now you have an array with all the lines in it. You can loop through it and do whatever you want. foreach($lines as $line){ $line=trim($line); if (!$conn) {exit("Connection Failed: " . $conn);} $sql="SELECT CUSTOMER_NUM, CUSTOMER_NAME, PHONE_1 FROM AR_CUST_MAST WHERE EMAIL_1 = '$line'"; //print $sql; $rs=odbc_exec($conn,$sql); if (!$rs) {exit("Error in SQL");} while (odbc_fetch_row($rs)) { $acct=odbc_result($rs,"CUSTOMER_NUM"); $name=odbc_result($rs,"CUSTOMER_NAME"); $phone=odbc_result($rs,"PHONE_1"); } $acct =mysql_real_escape_string($acct); $name =mysql_real_escape_string($name); $phone =mysql_real_escape_string($phone); $query = "insert into failed_messages values ('0','$acct', '$name', '$phone', '$line')"; mysql_query($query, $db); } //$myFile = "failed_messages/failed_messages.txt"; //$fh = fopen($myFile, 'w'); //fclose($fh); // erase file: file_put_contents('failed_messages/failed_messages.txt',''); ?> Quote Link to comment https://forums.phpfreaks.com/topic/238903-read-text-file/#findComment-1227602 Share on other sites More sharing options...
fugix Posted June 9, 2011 Share Posted June 9, 2011 Cool. Thanks. Here is the full code: <?php set_time_limit(900); ini_set('max_execution_time', '999'); include_once('inc/data.inc'); $file = file_get_contents('failed_messages/failed_messages.txt'); $lines = explode("\n",$file); // now you have an array with all the lines in it. You can loop through it and do whatever you want. foreach($lines as $line){ $line=trim($line); if (!$conn) {exit("Connection Failed: " . $conn);} $sql="SELECT CUSTOMER_NUM, CUSTOMER_NAME, PHONE_1 FROM AR_CUST_MAST WHERE EMAIL_1 = '$line'"; //print $sql; $rs=odbc_exec($conn,$sql); if (!$rs) {exit("Error in SQL");} while (odbc_fetch_row($rs)) { $acct=odbc_result($rs,"CUSTOMER_NUM"); $name=odbc_result($rs,"CUSTOMER_NAME"); $phone=odbc_result($rs,"PHONE_1"); } $acct =mysql_real_escape_string($acct); $name =mysql_real_escape_string($name); $phone =mysql_real_escape_string($phone); $query = "insert into failed_messages values ('0','$acct', '$name', '$phone', '$line')"; mysql_query($query, $db); } //$myFile = "failed_messages/failed_messages.txt"; //$fh = fopen($myFile, 'w'); //fclose($fh); // erase file: file_put_contents('failed_messages/failed_messages.txt',''); ?> excellent, please mark as solved, located in the lower left hand of the page Quote Link to comment https://forums.phpfreaks.com/topic/238903-read-text-file/#findComment-1227606 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.