CBaZ Posted September 23, 2007 Share Posted September 23, 2007 it writes the line or lines to be deleted correctly into issues_deleted.txt which is line fwrite($file_contents_deleted, $value); and when it refreshes the line still exists in banip.txt it does echo out the line as well just doesnt remove it from banip.txt anyone know why? <?php $file_contents = fopen("banip.txt", "r+"); // the file $file_contents_deleted = fopen("issues_deleted.txt", "r+"); // the file // [ DELETE ROUTINE ] if($_POST['action'] == "delete"){ // delete triggered $arrToDelete = $_POST['todelete']; // assign checked items array to var if($arrToDelete != "") { // if the array isnt open proceed foreach($arrToDelete as $key => $value) { // assign a key and value to //each checked item #$file_contents_deleted[count($file_contents_deleted)] = $file_contents[$key]; $file_contents_deleted[] = $file_contents[$key]; array_splice($file_contents, $key, 1, "");// assign null value to each print("Deleted ".$value.""); fwrite($file_contents_deleted, $value); for($i = 0; $i < count($file_contents); $i++) { // loop through the new if ($x == "file_contents_deleted") //or whatever, it's your comparison to see if this is a line to keep { $cow .= $x . "\n"; } fwrite($file_contents,$cow); } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/70353-solved-deleting-a-line-fromtxt/ Share on other sites More sharing options...
wildteen88 Posted September 23, 2007 Share Posted September 23, 2007 There are a few problems in your code. Try: <?php $ban_ips = array_map('trim', file('banip.txt')); $deleted_ips = file('issues_deleted.txt'); // [ DELETE ROUTINE ] // delete triggered if($_POST['action'] == "delete") { // if the array isnt open proceed if(isset($_POST['todelete']) && !empty($_POST['todelete'])) { $arrToDelete = $_POST['todelete']; // assign checked items array to var $handle['issues'] = fopen("issues_deleted.txt", "r+"); // assign a key and value to foreach($arrToDelete as $key => $ip2del) { if(in_array($ip2del, $ban_ips)) { $banip_key = array_keys($ban_ips, $ip2del); // remove banned ip from ban_ips array unset($ban_ips[$banip_key[0]]); print('Deleted: ' . $ip2del . "<br />\n"); // write deleted banned ip to issues_deleted.txt fwrite($handle['issues'], $ip2del ."\n"); } } fclose($handle['issues']); // open banip.txt and remove existing ips $handle['banip'] = fopen("banip.txt", "w"); // rewrite banip.txt with undeleted ips foreach($ban_ips as $ip) { fwrite($handle['banip'], $ip . "\n"); } fclose($handle['banip']); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/70353-solved-deleting-a-line-fromtxt/#findComment-353416 Share on other sites More sharing options...
CBaZ Posted September 23, 2007 Author Share Posted September 23, 2007 doesn't seem to be working Quote Link to comment https://forums.phpfreaks.com/topic/70353-solved-deleting-a-line-fromtxt/#findComment-353423 Share on other sites More sharing options...
wildteen88 Posted September 23, 2007 Share Posted September 23, 2007 What's happening when you run the script? Is it adding anything to any files. Need more info. Also could you post some code for your form. So I can understand how $_POST is formatted. I'm assuming your using checkboxs in your form which is named todelete[] for choosing which banned ips to delete from banip.txt. And that each ip address is one its own line in banip.txt Quote Link to comment https://forums.phpfreaks.com/topic/70353-solved-deleting-a-line-fromtxt/#findComment-353471 Share on other sites More sharing options...
CBaZ Posted September 23, 2007 Author Share Posted September 23, 2007 it's not writing to any files atm .. this is the form <?php // [ READ & OUTPUT FILE CONTENTS ] ?> <!-- The Form & Table --> <form name="frmls" method="POST" action="<?php $PHP_SELF ?>" style="margin:0;"> <input type="hidden" name="action" value="delete"> <table width="780" height="0" border="0" cellpadding="1" cellspacing="1" class="tablehead"> <tr> <td align="center" width="1%" class="rowA"><input name="chkAll" type="checkbox" id="ids[]" value="1" onClick="CheckAll();" title="Select All"></td> <td align="center" width="5%" class="rowA"><strong>IP</strong></td> <td align="center" width="5%" class="rowA"><strong>Date/Time</strong></td> <td align="center" width="5%" class="rowA"><strong>Browser</strong></td> <td align="center" width="5%" class="rowA"><strong>Hostname</strong</td> </tr> <?php $file_contents = file("banip.txt"); // the file foreach($file_contents as $key=>$value){ // assign a key to each line in the file list($entry[0],$entry[1],$entry[2],$entry[3]) = split('[,|]',$value); $entries[]=array('key'=>$key, 'ip'=>$entry[0], 'date'=>$entry[1], 'hostname'=>$entry[2], 'browser'=>$entry[3]); print "<table width=\"780\" height=\"0\" border=\"0\" cellpadding=\"1\" cellspacing=\"1\" class=\"tablehead\"> <tr> <td align=\"center\" width=\"1%\" class=\"rowB\"><input name=\"todelete[$key]\" type=\"checkbox\" id=\"ids[]\" value=\"$value\" onClick=\"this.form.chkAll.checked=false; \"></td> <td align=\"left\" width=\"5%\" class=\"rowB\"><strong>".$entry[0]."</strong></td> <td align=\"left\" width=\"5%\" class=\"rowB\"><strong>".$entry[1]."</strong></td> <td align=\"left\" width=\"5%\" class=\"rowB\"><strong>".$entry[2]."</strong></td> <td align=\"left\" width=\"5%\" class=\"rowB\"><strong>".$entry[3]."</strong></td> </tr>\n"; } ?> <tr> <td colspan="8" align="center" class=\"rowB\"><input type="submit" onClick="CheckIfChecked();" value="Remove IP"></td> </tr> </table> </form> </td> </table> Quote Link to comment https://forums.phpfreaks.com/topic/70353-solved-deleting-a-line-fromtxt/#findComment-353480 Share on other sites More sharing options...
wildteen88 Posted September 23, 2007 Share Posted September 23, 2007 I see, banip.txt does not hold just the ip address but other info. My code still works fine! Could you also post example data for banip.txt too. Quote Link to comment https://forums.phpfreaks.com/topic/70353-solved-deleting-a-line-fromtxt/#findComment-353530 Share on other sites More sharing options...
CBaZ Posted September 23, 2007 Author Share Posted September 23, 2007 example data for banip.txt ... its in another folder the banip.txt would it matter? i changed these values back of course in my file in your code. 65.125.151.21, 09.09.2007 01:41:18, Opera/9.0 (Windows NT 5.1; U; en), host-65-125-151-21.lextron ip date time browser host Quote Link to comment https://forums.phpfreaks.com/topic/70353-solved-deleting-a-line-fromtxt/#findComment-353535 Share on other sites More sharing options...
wildteen88 Posted September 24, 2007 Share Posted September 24, 2007 example data for banip.txt ... its in another folder the banip.txt would it matter? Yes it would! What is the path to banip.txt and the path to the script you place my code in? Both files will have to be in the same folder as each other or you'll need to specify the path to banip.txt (and issues_deleted.txt) in my script for the file() and fopen() functions. Also with that example data my code works no problem. The issue is to do with the script not finding banip.txt if its in a different folder than the script. Quote Link to comment https://forums.phpfreaks.com/topic/70353-solved-deleting-a-line-fromtxt/#findComment-354157 Share on other sites More sharing options...
CBaZ Posted September 26, 2007 Author Share Posted September 26, 2007 banip.txt and issues_deleted.txt are in public_html/banip the banip.php file is in public_html/password how would i make the banip.php file link correctly to the .txts Quote Link to comment https://forums.phpfreaks.com/topic/70353-solved-deleting-a-line-fromtxt/#findComment-355441 Share on other sites More sharing options...
wildteen88 Posted September 26, 2007 Share Posted September 26, 2007 Add ../banip/ before any instance of banip.txt or issues_deleted.txt in the file and fopen functions. Quote Link to comment https://forums.phpfreaks.com/topic/70353-solved-deleting-a-line-fromtxt/#findComment-355855 Share on other sites More sharing options...
CBaZ Posted September 27, 2007 Author Share Posted September 27, 2007 yes have tryed that also still no result .. it does not print out nor does it delete the line and no error is given either anyway we can try an error echo out? Quote Link to comment https://forums.phpfreaks.com/topic/70353-solved-deleting-a-line-fromtxt/#findComment-356143 Share on other sites More sharing options...
wildteen88 Posted September 27, 2007 Share Posted September 27, 2007 Not sure whats going on. Added in some echo's to see what is going on during script execution. <?php error_reporting(E_ALL); ini_set('display_errors', 'On'); $banip_file = '../banip/banip.txt'; $delip_file = '../banip/issues_deleted.txt'; if(file_exists($banip_file)) { echo $banip_file . ' exists'; if(is_readable($banip_file)) echo ' and is readable'; else die(' is not readable'); if(is_writable($banip_file)) echo ' and writable'; else die(' is not writable'); } else { die($banip_file . ' does not exist in ' . dirname(__FILE__)); } if(file_exists($delip_file)) { echo "\n<br />" . $delip_file . ' exists'; if(is_readable($delip_file)) echo ' and is readable'; else die(' is not readable'); if(is_writable($delip_file)) echo ' and writable'; else die(' is not writable'); } else { die($delip_file . ' does not exist in ' . dirname(__FILE__)); } echo "\n<p>Opening $banip_file<br />\nFile contents:\n"; $ban_ips = array_map('trim', file($banip_file)); echo '<pre>' . print_r($ban_ips, true) . "</pre></p>\n<p>Opening $delip_file<br />\nFile contents:\n"; $deleted_ips = file($delip_file); echo '<pre>' . print_r($deleted_ips, true) . "</pre></p>\n"; // [ DELETE ROUTINE ] // delete triggered if(isset($_POST['action']) && $_POST['action'] == "delete") { echo '<p>Entered delete routine<p><pre>' . print_r($_POST, true) . "</pre></p>\n"; // if the array isnt open proceed if(isset($_POST['todelete']) && !empty($_POST['todelete'])) { $arrToDelete = $_POST['todelete']; // assign checked items array to var echo '<p>Opening ' . $delip_file . '</p>'; $handle['issues'] = fopen($delip_file, "r+"); // assign a key and value to foreach($arrToDelete as $key => $ip2del) { if(in_array($ip2del, $ban_ips)) { echo '<p><b>' . $ip2del . '</b> exists in <b>' . $banip_file . "</b><br />\n"; echo 'Getting key from $ban_ips<br />'; $banip_key = array_keys($ban_ips, $ip2del); echo 'key retrived - Key:' . $banip_key[0] . "<br />\n"; // remove banned ip from ban_ips array unset($ban_ips[$banip_key[0]]); echo 'Deleted: ' . $ip2del; // write deleted banned ip to issues_deleted.txt fwrite($handle['issues'], $ip2del ."\n"); echo ' and has been written to ' . $delip_file . "</p>\n"; } } fclose($handle['issues']); echo "<p>Closed $delip_file</p>\n<p>Opening $banip_file</p>\n<p>"; // open banip.txt and remove existing ips $handle['banip'] = fopen($banip_file, "w"); // rewrite banip.txt with undeleted ips foreach($ban_ips as $ip) { echo "Writting $ip to $banip_file<br />\n"; fwrite($handle['banip'], $ip . "\n"); } fclose($handle['banip']); echo "</p>\n<p>Closing $banip_file</p>"; } } else { echo 'No post data!<pre>' . print_r($_POST, true) . '</pre>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/70353-solved-deleting-a-line-fromtxt/#findComment-356576 Share on other sites More sharing options...
CBaZ Posted September 29, 2007 Author Share Posted September 29, 2007 ok here we go also this code doesn't seem to echo out the line to the user that it's being deleted my original code did. not sure why that is but may be a sign hinting its not getting somehing. ../banip/banip.txt exists and is readable and writable ../banip/issues_deleted.txt exists and is readable and writable Opening ../banip/banip.txt File contents: Array ( ** REMOVED ** ) Opening ../banip/issues_deleted.txt File contents: Array ( ) Entered delete routine Array ( [action] => delete [todelete] => Array ( [0] => 66.162.135.6, 07.09.2007 23:01:40, Opera/9.0 (Windows NT 5.1; U; en), NA ) ) Opening ../banip/issues_deleted.txt Closed ../banip/issues_deleted.txt Opening ../banip/banip.txt ** REMOVED ** Closing ../banip/banip.txt Quote Link to comment https://forums.phpfreaks.com/topic/70353-solved-deleting-a-line-fromtxt/#findComment-357956 Share on other sites More sharing options...
wildteen88 Posted September 29, 2007 Share Posted September 29, 2007 Umm... Change this line: <td align=\"center\" width=\"1%\" class=\"rowB\"><input name=\"todelete[$key]\" type=\"checkbox\" id=\"ids[]\" value=\"$value\" onClick=\"this.form.chkAll.checked=false; \"></td> in your form to this: <td align=\"center\" width=\"1%\" class=\"rowB\"><input name=\"todelete[$key]\" type=\"checkbox\" id=\"ids[]\" value=\"" . trim($value) . "\" onClick=\"this.form.chkAll.checked=false; \"></td> Also add the following lines: else { echo '</b> does not exist in <b>' . $file['banip'] . "</b></div>\n"; echo '<p><b>' . $ip2del . '</b> exists in <b>' . $banip_file . "</b><br />\n"; } After these lines: // write deleted banned ip to issues_deleted.txt fwrite($handle['issues'], $ip2del . "\n"); } In banip.php. Re-run banip.php again. Quote Link to comment https://forums.phpfreaks.com/topic/70353-solved-deleting-a-line-fromtxt/#findComment-358003 Share on other sites More sharing options...
CBaZ Posted September 29, 2007 Author Share Posted September 29, 2007 it was deleted after all but doesnt echo out the line above the table still I think. here is the read out ../banip/banip.txt exists and is readable and writable ../banip/issues_deleted.txt exists and is readable and writable Opening ../banip/banip.txt File contents: Array ( ** REMOVED ** ) Opening ../banip/issues_deleted.txt File contents: Array ( ) Entered delete routine Array ( [action] => delete [todelete] => Array ( [0] => xx.xxx.xxx.x, 07.09.2007 23:01:40, Opera/9.0 (Windows NT 5.1; U; en), NA ) ) Opening ../banip/issues_deleted.txt x.xxx.xxx.x, 07.09.2007 23:01:40, Opera/9.0 (Windows NT 5.1; U; en), NA exists in ../banip/banip.txt Getting key from $ban_ips key retrived - Key:0 Deleted: x.xxx.xxx.x, 07.09.2007 23:01:40, Opera/9.0 (Windows NT 5.1; U; en), NA and has been written to ../banip/issues_deleted.txt Closed ../banip/issues_deleted.txt Opening ../banip/banip.txt ** REMOVED ** Closing ../banip/banip.txt[code] [/code] Quote Link to comment https://forums.phpfreaks.com/topic/70353-solved-deleting-a-line-fromtxt/#findComment-358015 Share on other sites More sharing options...
marcus Posted September 29, 2007 Share Posted September 29, 2007 You shouldn't post people's IPs like this. Quote Link to comment https://forums.phpfreaks.com/topic/70353-solved-deleting-a-line-fromtxt/#findComment-358025 Share on other sites More sharing options...
wildteen88 Posted September 29, 2007 Share Posted September 29, 2007 Its now found a match for the selected ip(s) to delete as it is reporting it in the debug information: Opening ../banip/issues_deleted.txt 66.162.135.6, 07.09.2007 23:01:40, Opera/9.0 (Windows NT 5.1; U; en), NA exists in ../banip/banip.txt Getting key from $ban_ips key retrived - Key:0 Deleted: 66.162.135.6, 07.09.2007 23:01:40, Opera/9.0 (Windows NT 5.1; U; en), NA and has been written to ../banip/issues_deleted.txt Closed ../banip/issues_deleted.txt has issues_deleted.txt and banips.txt been updated now? Quote Link to comment https://forums.phpfreaks.com/topic/70353-solved-deleting-a-line-fromtxt/#findComment-358026 Share on other sites More sharing options...
CBaZ Posted September 29, 2007 Author Share Posted September 29, 2007 Thank You. It's Working Great Quote Link to comment https://forums.phpfreaks.com/topic/70353-solved-deleting-a-line-fromtxt/#findComment-358091 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.