Deleting last line in text file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Deleting last line in text file

Post by guitarlvr »

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);
?>
orbitz
Forum Newbie
Posts: 3
Joined: Tue Jun 10, 2008 10:54 am

Re: Deleting last line in text file

Post by orbitz »

how about using the function file() to read the file into array instead of using file_get_contents() and then use array_pop to pop out the last element of the array which is the last line?
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Re: Deleting last line in text file

Post by guitarlvr »

orbitz wrote:how about using the function file() to read the file into array instead of using file_get_contents() and then use array_pop to pop out the last element of the array which is the last line?
That looks to be exactly what i needed. I didn't know there was a function already built in! Thanks very much.
rmessa
Forum Newbie
Posts: 2
Joined: Wed Jun 04, 2008 9:39 am

Re: Deleting last line in text file

Post by rmessa »

Code: Select all

 
$content = file_get_contents($path);
$lines = explode('\n', $content);
$count = count($lines) - 1;
for($i0 = 0; $i0 < $count; $i0++)
$newContent .= $lines[$i0];
 
http://mobile.sybrain.com
ERP for WAP
Post Reply