Jump to content

Delete/Remove Last 5 Lines from Text file


defonic

Recommended Posts

Extremely new here and to PHP and I have exhausted my newbie resources and my brain on this one.  :'(

 

Concept: Cron Job to INSERT 5 lines per hour (Not the whole list at once)

[*]Script to read a pipe delimited text file (about 5k Lines)

[*]Open File Get last (bottom) 5 lines of file, ignore the rest

[*]Insert those 5 lines into my database

[*]Open File, Remove last (bottom) 5 lines

 

I am able to do parts 1-3 but not part 4 (removing last 5 lines) Please help me!

 



$data = array_slice(file('data.txt'), -5);
foreach($data as $value)
{
  $fields=explode("|",$value);
  $num=count($fields);
  for ($n=0;$n<$num;$n++)
  {
    $null="";
    print ($n==0) ? "Importing Video...<BR>" : $null;
  }

// Insert into tmpvideos table here
include("includes/config.php");
            $last_id_result = @mysql_query( @"SELECT ext_id FROM tmpvideos ORDER BY ext_id+0 DESC, id DESC" );
            if ( 0 < @mysql_num_rows( $last_id_result ) )
            {
                $last_id = @mysql_result( $last_id_result, 0, "ext_id" ) + 1;
            }
            else
            {
                $last_id = "1";
            }


            copy($fields[3], "thumbs/ex_".$last_id.".jpg" );
            $fields[3] = "thumbs/ex_".$last_id.".jpg";

set_time_limit(0);
mysql_query("INSERT into tmpvideos (id, ext_id, title, description, duration, category, date_added, thumb, video, views, rating, votes, status)
VALUES ('','".$last_id."','$fields[0]','$fields[1]','$fields[5]','$fields[6]',NOW(),'$fields[3]','$fields[2]','','','','1')");

}

 

I understand the 'concept' but am unable to grasp the code! I have tried many snippets and really need help.

Link to comment
Share on other sites

should be something like:

$data = array_slice(file('data.txt'), 0, -5, true));

 

With your help I solved it, Thank you!  :D

 

$alines=file('data.txt');
$dlines=array_slice($alines,0, -5);
$fileContents=implode($dlines);
$fp=fopen('data.txt','w+');
fwrite($fp,$fileContents);
fclose($fp); 

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.