Page 1 of 1

delete data from file

Posted: Thu Dec 01, 2005 8:17 pm
by jabbaonthedais
Hey guys, I've been looking for a function to delete data from a file but having trouble. Can someone link me to a function to set me on the right track?

My file is a .htpasswd file so the contents look like this:

joesmith01:rlY/Eb2xa0uYs
johnny92:rlO0/FP9K1Dqc
example3:rlbVJNbKNDKc2

I want to be able to delete users (the user:pass line) via my script. Would I pull the data as an array and use array_splice() or what would you guys think?

Thanks!

Posted: Thu Dec 01, 2005 8:44 pm
by John Cartwright
use file to seperate each line into an array, then it is a matter of matching the string your using (preferably case sensitive) so you'll need to use stristr, delete the file as well as the array indice which matched your search, and rewrite it.

Posted: Thu Dec 01, 2005 9:08 pm
by jabbaonthedais
Jcart wrote:use file to seperate each line into an array, then it is a matter of matching the string your using (preferably case sensitive) so you'll need to use stristr, delete the file as well as the array indice which matched your search, and rewrite it.
So I need to delete the file and rewrite it using the array right? stristr() isolates the line from the array, then unset() that from the array, then rewrite the array to a file? Sorry if I'm misunderstanding.

Posted: Thu Dec 01, 2005 9:11 pm
by John Cartwright
bang on!

Posted: Sat Dec 03, 2005 4:44 pm
by jabbaonthedais
Ok, I kept getting problems so I narrowed it down to the unset() function. I'm using the example on php.net and have no idea why its not working. I need to be able to delete single elements of an array, while leaving the array intact.

Code: Select all

<?php
$array = array("red", "green", "blue", "yellow", "black");
echo "Before: " . $array[0] . ", " . $array[1] . ", " . $array[2] . ", " . $array[3] . ", " . $array[4];
unset($array['green']);
echo "<br>After: " . $array[0] . ", " . $array[1] . ", " . $array[2] . ", " . $array[3] . ", " . $array[4];
?>

Returns:
Before: red, green, blue, yellow, black
After: red, green, blue, yellow, black

The example given at php.net is this:

Code: Select all

// destroy a single element of an array
unset($bar['quux']);

Posted: Sat Dec 03, 2005 6:31 pm
by Jenk
green is not a valid key for that array, it is a value, not a key :)

Posted: Sat Dec 03, 2005 6:33 pm
by jabbaonthedais
Well what function would I use to delete a value from an array?

Posted: Sat Dec 03, 2005 6:38 pm
by Jenk
Take a look in the php.net manual. (function reference)

Posted: Sun Dec 04, 2005 12:08 am
by jabbaonthedais
I hate to keep posting about this but I think I've been looking at this code so long I can't see whatever is wrong.

I can't get the stristr() function to find my results from the values of my array. Here is my code:

Code: Select all

<?php
$date = date("Ymd");
$db = 'XXXXXX'; // mySQL database name
$duser = 'XXXXXX'; // mySQL username
$dpw = 'XXXXX'; // mySQL password
$mysql_access = mysql_connect("localhost", $duser, $dpw);
mysql_select_db($db, $mysql_access);

$queryz2 = "SELECT * FROM promos WHERE expiration > $date ";
$resultz2 = mysql_query($queryz2, $mysql_access);

$lines = file('.htpasswd');

echo "Array Before:<br>" . $lines[0] . "<br>" . $lines[1] . "<br>" . $lines[2] . "<br>" . $lines[3] . "<br>" . $lines[4] . "<p>";
/* returns:
john9382x35:rlY/Eb2xa0uYs 
johnny92:rlO0/FP9K1Dqc 
exp333:rlbVJNbKNDKc2 
user911:rlSRB4ZmA.W1U 
*/

$i = 1;
   while($row = mysql_fetch_row($resultz2))
  {
$f1[$i] = $row[0];
$f2[$i] = $row[1];
$f3[$i] = $row[2];
$f4[$i] = $row[3];
}

echo "test user: " . $f3[1];  // returns "johnny92"

$i = 0;
$z = 1;
foreach ($lines as $line_num => $line) {

$fd1 = stristr($lines[$i], $f3[0]);
$fd2 = stristr($lines[$i], $f3[1]);
$fd3 = stristr($lines[$i], $f3[2]);
$fd4 = stristr($lines[$i], $f3[3]);

echo "<p>matched results:<br>" . $fd1 . "<br>" . $fd2 . "<br>" . $fd3 . "<br>" . $fd4 . "<p>";  // returns nothing

if ($lines[$i] == $fd1 or $lines[$i] == $fd2 or $lines[$i] == $fd3 or $lines[$i] == $fd4) {

unset($lines[$i]);
$lines = array_values($lines);
$z++;
$i++;
}
}

echo "Array After:<br>" . $lines[0] . "<br>" . $lines[1] . "<br>" . $lines[2] . "<br>" . $lines[3] . "<br>" . $lines[4] . "<p>";
/* returns:
john9382x35:rlY/Eb2xa0uYs 
johnny92:rlO0/FP9K1Dqc 
exp333:rlbVJNbKNDKc2 
user911:rlSRB4ZmA.W1U 
*/

?>
My test username is johnny92. Thats what $f3[1] shows before the stristr function, then it shows nothing. Basically I need to get the stristr() to give me the full array value for johnny92 (which would be johnny92:rlO0/FP9K1Dqc, so I can remove it from the array.

Posted: Mon Dec 05, 2005 11:47 am
by jabbaonthedais
This is what is not working:

Code: Select all

$fd1 = stristr($lines[$i], $f3[0]);
Is this because $lines[$i] is a value of an array and not a string? Or isn't it a string in this case? :(