defonic Posted August 31, 2011 Share Posted August 31, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246112-deleteremove-last-5-lines-from-text-file/ Share on other sites More sharing options...
WebStyles Posted August 31, 2011 Share Posted August 31, 2011 if you know how to get the last five lines, do the reverse, get all the lines except the last five, and write the whole thing to the file (replacing all content) Quote Link to comment https://forums.phpfreaks.com/topic/246112-deleteremove-last-5-lines-from-text-file/#findComment-1263946 Share on other sites More sharing options...
defonic Posted August 31, 2011 Author Share Posted August 31, 2011 Every time I try I end up removing everything except the last 5 lines. Quote Link to comment https://forums.phpfreaks.com/topic/246112-deleteremove-last-5-lines-from-text-file/#findComment-1263951 Share on other sites More sharing options...
WebStyles Posted August 31, 2011 Share Posted August 31, 2011 should be something like: $data = array_slice(file('data.txt'), 0, -5, true)); Quote Link to comment https://forums.phpfreaks.com/topic/246112-deleteremove-last-5-lines-from-text-file/#findComment-1263956 Share on other sites More sharing options...
defonic Posted August 31, 2011 Author Share Posted August 31, 2011 should be something like: $data = array_slice(file('data.txt'), 0, -5, true)); With your help I solved it, Thank you! $alines=file('data.txt'); $dlines=array_slice($alines,0, -5); $fileContents=implode($dlines); $fp=fopen('data.txt','w+'); fwrite($fp,$fileContents); fclose($fp); Quote Link to comment https://forums.phpfreaks.com/topic/246112-deleteremove-last-5-lines-from-text-file/#findComment-1263977 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.