Problem:

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
Cleptomain
Forum Newbie
Posts: 2
Joined: Thu Jan 30, 2003 1:14 pm

Problem:

Post by Cleptomain »

Ahoi

I'm coding a script ... i have a lil probleme :lol: :

i want to read a file (with the file() function) and then search for an array element and delete this element from the array ... but i'm using a variable for the word that the elemnt has to be.... (the variable is $dat)

my code looks like this:

Code: Select all

<? if($dat)&#123;
if(@unlink("upload/".$dat))&#123;
$zahl = file("count.txt");
$zohl = $zahl&#1111;0]-1;
$z = fopen("count.txt","w");
fwrite($z,$zohl);
fclose($z);
$zahl2 = file("avas.txt");

# above does my probleme take place :-/ #

echo "The file $dat has been deleted!";
&#125;else&#123;
echo "Error: File has not been deleted!";
&#125;
but now i'm stuckin :-/

can somebody help me plz ?
-thanks
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

..something like this maybe?

Code: Select all

<?php
 if($dat) {
   if(@unlink('upload/'.$dat)){
   $zahl = file('count.txt');
   $zohl = (( (int) $zahl[0] ) -1);
   $z = fopen('count.txt','w');
   fwrite($z,$zohl);
   fclose($z);

   $filelist = file('avas.txt');
   $dkey = array_search($dat,$filelist);
   if ($dkey) {
      unset( $filelist[$dkey] );
      $z = fopen('avas.txt','w');
      foreach ($filelist as $filename) fwrite($z,$filename."\n");
      fclose($z);
   } else {
      # didnt find it..
   }
  
   echo "The file $dat has been deleted!";
 }else{
   echo "Error: File has not been deleted!";
 } 
?>
I don't recall if file includes line endings or not, if so you want to add that to the search instead of the write..
Cleptomain
Forum Newbie
Posts: 2
Joined: Thu Jan 30, 2003 1:14 pm

Post by Cleptomain »

Yeah, thanks .. thats exatcly what i searched for :D
Post Reply