Jump to content

Read text file


jakebur01

Recommended Posts

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

 

Link to comment
Share on other sites

$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','');

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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()

Link to comment
Share on other sites

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','');



?>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.