Deleting last line in text file
Posted: Tue Jun 10, 2008 10:47 am
On a scheduled basis, i am automatically downloaded three files from the US OFAC Department. (terrorism list) Currently i have the script working and am grabbing content of all three files, writting it to a file on my server, then sending an email with the status of the files. The last line of each of the fixed format text files is a end of file marker which is causing the guys that are picking this file up all kinds of problems. From the code below, how would i be able to delete the last line of each of the three text files?
Code: Select all
<?php
//Define script variables
//$ofac["SDN"] = "http://www.ustreas.gov/offices/enforcement/ofac/sdn/delimit/tsdn.ff";
//$ofac["ADD"] = "http://www.ustreas.gov/offices/enforcement/ofac/sdn/delimit/tadd.ff";
$ofac["ALT"] = "http://www.ustreas.gov/offices/enforcement/ofac/sdn/delimit/alt.ff";
$destination = "C:/OFAC/";
$status["SDN"] = '';
$status["ADD"] = '';
$status["ALT"] = '';
$to = 'wayne@example.com';
$message = '
<html>
<body>
<p>Below is the status of the OFAC data download for ' . date('l, F jS Y') . '</p>
<p><table border="1" cellpadding="3">
<tr>
<th align="center">Report:</th>
<th align="center">Successful:</th>
<th align="center">Could not grab file:</th>
<th align="center">Could not create file:</th>
<th align="center">Could not write to the file:</th>
</tr>';
foreach ($ofac as $key => $value)
{
$filename = basename($value);
if (file_exists($filename))
{
unlink($destination . $filename);
}else{
$file = file_get_contents($value);
if ($file == FALSE)
{
$status["$key"] = '1';
}else{
$fh = fopen($destination . $filename, 'w+');
if ($fh == FALSE)
{
$status["$key"] = '2';
}else{
$copy = fputs($fh, $file);
if ($copy == FALSE)
{
$status["$key"] = '3';
}else{
if ($status['$key'] == '')
{
$status["$key"] = '4';
}
fclose($fh);
unset($file);
}
}
}
}
unset($key, $value);
}
//Build rest of message.
foreach($status as $key => $value)
{
if ($status["$key"] == '1')
{
$message .= '
<tr>
<th align="center">' . $key . '</th>
<td align="center">-</td>
<td align="center"><font color="red">X</font></td>
<td align="center">-</td>
<td align="center">-</td>
</tr>';
}
if ($status["$key"] == '2')
{
$message .= '
<tr>
<th align="center">' . $key . '</th>
<td align="center">-</td>
<td align="center">-</td>
<td align="center"><font color="red">X</font></td>
<td align="center">-</td>
</tr>';
}
if ($status["$key"] == '3')
{
$message .= '
<tr>
<th align="center">' . $key . '</th>
<td align="center">-</td>
<td align="center">-</td>
<td align="center">-</td>
<td align="center"><font color="red">X</font></td>
</tr>';
}
if ($status["$key"] == '4')
{
$message .= '
<tr>
<th align="center">' . $key . '</th>
<td align="center"><font color="green">X</font></td>
<td align="center">-</td>
<td align="center">-</td>
<td align="center">-</td>
</tr>';
}
}
$message .= '</table></p>
<p><b>Successfull:</b> Data has been successfully downloaded.<br>
<b>Could not grab file:</b> File is not able to be found. Check that the link has not changed.<br>
<b>Could not create file:</b> Could not create the file to be written on the server. Check that security has not changed.<br>
<b>Could not write to the file:</b> Could not write to the newly created file. Check that security has not changed.</p>
</body>
</html>';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: administrator@example.com';
$subject = 'OFAC Data Download';
mail ($to, $subject, $message, $headers);
?>