#1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Posts
    0
    Rep Power
    0

    Thumbs down More trouble in paradise - editing files


    I've been playing around with this code for a bit, but I'm stumped:

    PHP Code:
    <?

       
    // Start the form.
    print ("<FORM ACTION=\"$PHP_SELF\" METHOD=\"POST\">\n");
    print (
    "<input type=hidden name=action value=edit>\n");
    print (
    "<TR><TD><B>File Name</B></TD><TD></TD></TR><br>\n");



    // Read the files from the directory.
    $Open opendir ("users");
    while (
    $Files readdir ($Open)) {
         
    $Filename "users/" $Files;
         if (
    is_file ($Filename)) {
             
    $Size filesize ("users/$Files");
             print (
    "<TR><TD><a href=\"$Filename\">$Files</a></TD><TD></TD><TD><INPUT TYPE=CHECKBOX NAME=\"edit\" VALUE=\"$Files\"></TD><TR><BR>\n");
         }
    }
    closedir ($Open);

    print (
    "<TR><TD COLSPAN=4 ALIGN=CENTER><INPUT TYPE=SUBMIT NAME=\"SUBMIT\" VALUE=\"Submit!\"></FORM></TD></TR>\n");



         if(
    $action == "edit")
        {

    include 
    "$Filename";
    echo 
    "<form action='$PHP_SELF' method=POST>";
    echo 
    "Username: <input type=text size=15 name=username value=$username><br>";
    echo 
    "Password: <input type=password size=15 name=newpassword value=\"$password\"><br>";
    echo 
    "<input type=\"hidden\" name=\"action\" value=\"config\"><br>";
    echo 
    "Email Address: <input type=text size=15 name=newemail value=\"$email\"><br>";
    echo 
    "Allowed to post? <input type=text size=15 name=nowcanpost value=\"$canpost\"><br>";
    echo 
    "Admin? <input type=text name=isadmin value=\"$admin\"><br><p>";
    echo 
    "<input type=submit value=edit name=submit> </form>";


         if(
    $action == "config")
        {

    $file "$Filename";
    $content file($file);
    $num count($content);
    //for($i=-; $i<$num; $i++)
    for($i=0;$i<$num;$i++)

    {
        if(
    ereg('\$password',$content[$i]))
        {
            
    $content[$i] = "\$password = \"$newpassword\";";
        }
        if(
    ereg('\$admin',$content[$i]))
        {
            
    $content[$i] = "\$admin = \"$isadmin\";";
        }
        if(
    ereg('\$canpost',$content[$i]))
        {
            
    $content[$i] = "\$canpost= \"$nowcanpost\";";
        }

    }
    $data join("\r\n",$content);
    $fp fopen($file,'w');
    fwrite($fp,$data);
    fclose($fp);

    echo 
    "<br><table width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
    echo 
    "<tr><td>\n";
    echo 
    "Thank You!!!\n";
    echo 
    "Your details should now be changed\n";
    echo 
    " <td></tr></table>\n";
    }

    }
    ?>

    Once you select which file to edit it simply opens up the last file in the list, I assume that it is a problem with my $Files variable


    But I'm stumped.

    Incidentally it doesn't even update the last file once you submit the changes

    point of reference - the files I'm editing look like this:

    PHP Code:

     <?
    $username 
    "Craig1";
    $password "test";
    $email "me@mydomain.com";
    $canpost "yes";
    $admin "yes";
    $ip "xxx.xx.xx.xxx";
      
    ?>
    but some of you will already know that from my previous troubles.

    Any thoughts?
  2. #2
  3. phpkid ~~~~~~ :o)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Nov 2000
    Location
    NJ, USA
    Posts
    2,534
    Rep Power
    20
    hello,

    i have a quick glance at your code and i can see that 'IF' condition of checking the action against 'config' is inside IF condition where it checks whether $action is 'edit'. so it would never get in to if block of 'config' even if your action is CONFIG. so close the IF block having condition action == edit just before your next if block. ok ???

    and also , instead of ereg you can use strstr.

    just do this,strstr($content[i],'$password') and put it instead of ereg.

    this is becoz you are not actually trying to look for pattern inside a string but you are looking for simpe string and here strstr would be faster than ereg. ok ??

    hope that helps,
    jd
    _____________________________
    d.k.jariwala (JD)
    ~ simple thought, simple act ~
    I blog @ http://jdk.phpkid.org
  4. #3
  5. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Posts
    0
    Rep Power
    0

    eek


    Me Ducks as everything flies over my head............

    close it?

    ?>
    and re open?

    <?

    Is that what you mean? I did that and still i get the same:

    it picks up the info from the last file listed in the directory and doesn't edit it
  6. #4
  7. phpkid ~~~~~~ :o)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Nov 2000
    Location
    NJ, USA
    Posts
    2,534
    Rep Power
    20
    fine,

    i am telling you to check this,
    PHP Code:

         
    if($action == "edit")
        {

    include 
    "$Filename";
    echo 
    "<form action='$PHP_SELF' method=POST>";
    echo 
    "Username: <input type=text size=15 name=username value=$username><br>";
    echo 
    "Password: <input type=password size=15 name=newpassword value=\"$password\"><br>";
    echo 
    "<input type=\"hidden\" name=\"action\" value=\"config\"><br>";
    echo 
    "Email Address: <input type=text size=15 name=newemail value=\"$email\"><br>";
    echo 
    "Allowed to post? <input type=text size=15 name=nowcanpost value=\"$canpost\"><br>";
    echo 
    "Admin? <input type=text name=isadmin value=\"$admin\"><br><p>";
    echo 
    "<input type=submit value=edit name=submit> </form>";

    //see i can see that your next if block where it checks $action against "config" is inside the if block of $action=="edit". right ??? so this would be never executed even though $action is set to 'config." so end up if block here only. like this..

    }

    // so its closed. now read my previous answer and get what i am talking about. sorry if i was confusing  !!! 


         
    if($action == "config")
        { 
    got it ???

    jd
    _____________________________
    d.k.jariwala (JD)
    ~ simple thought, simple act ~
    I blog @ http://jdk.phpkid.org
  8. #5
  9. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Posts
    0
    Rep Power
    0
    d'oh I'm such a pillock.

    ok that works as far as the editing of the file goes, but no matter which file i choose it gives me the details for the final one in the directory, which it also edits as well:


    The page looks like this with check boxes at each file.
    • Craig1.inc.php
      raul.inc.php
      Opus.inc.php
      index.php
      Skol.inc.php
      bleed.inc.php
      James.inc.php


    so if I check Craig1.inc.php it opens James.inc.php and if i make changes, that is the file that is changed.

    I assume that this is related to my code that looks like this

    PHP Code:
    TR><TD><a href=\"$Filename\">$Files</a></TD><TD></TD><TD><INPUT TYPE=CHECKBOX NAME=\"edit\" VALUE=\"$Files\"></TD><TR><BR>\n"); 
    I guess this is the problem as this is listing all the files, and not giving each individual one a name.
  10. #6
  11. phpkid ~~~~~~ :o)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Nov 2000
    Location
    NJ, USA
    Posts
    2,534
    Rep Power
    20
    yeah,

    this part is wrong.

    <TR><TD><a href="$Filename\">$Files</a></TD><TD></TD><TD><INPUT TYPE=CHECKBOX NAME=\"edit\" VALUE=\"$Files\"></TD><TR><BR>\n");

    here name of all checkbox is edit !!! you should have different names there. this is not a radio button. okie ?

    so do like

    PHP Code:
    $Open opendir ("users");
    $i 0;
    while (
    $Files readdir ($Open)) {
         
    $Filename "users/" $Files;
         if (
    is_file ($Filename)) {
             
    $Size filesize ("users/$Files");
             print (
    "<TR><TD><a href=\"$Filename\">$Files</a></TD><TD></TD><TD><INPUT TYPE=CHECKBOX NAME=\"edit$i\" VALUE=\"$Files\"></TD><TR><BR>\n");
         }
    }
    closedir ($Open); 
    so this would name checkboxes edit0,edit1, edit2.

    btw in your file you are just checking one file , right ?? so why you are putting up checkboxes. if you are putting up checkboxes then you should check status of each and every checkbox.

    and also,

    what is this ??
    PHP Code:
    $file "$Filename";
    $content file($file);
    $num count($content); 
    cant u directly use,

    PHP Code:
    $content file($Filename); 
    tell me !!!

    i think u need to revise your code from ground up.

    i give u one advice. put up the code which edits the file inside a function. and then just call it. this way when you have to edit multiple files you just pass it to function.

    now i hope you got some idea and solve out problem on your own,

    jd
    _____________________________
    d.k.jariwala (JD)
    ~ simple thought, simple act ~
    I blog @ http://jdk.phpkid.org
  12. #7
  13. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Posts
    0
    Rep Power
    0
    Thanks, I'll see how I go
  14. #8
  15. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Joliette, Québec, Canada
    Posts
    6
    Rep Power
    0
    PHP Code:
    $Open opendir ("users");
    $i 0;
    while (
    $Files readdir ($Open))
    {
        
    $Filename "users/" $Files;
        if (
    is_file ($Filename))
        {
            
    $Size filesize ("users/$Files");
            print (
    "<TR><TD><a href=\"$Filename\">$Files</a></TD><TD></TD><TD><INPUT TYPE=CHECKBOX NAME=\"edit$i\" VALUE=\"$Files\"></TD><TR><BR>\n");
        }
    }
    closedir ($Open); 
    hmm jdk you forgot to increment $i.... You should add $i ++; in the if block.
  16. #9
  17. phpkid ~~~~~~ :o)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Nov 2000
    Location
    NJ, USA
    Posts
    2,534
    Rep Power
    20
    yup, i missed it,

    thx for correcting,

    jd
    _____________________________
    d.k.jariwala (JD)
    ~ simple thought, simple act ~
    I blog @ http://jdk.phpkid.org

IMN logo majestic logo threadwatch logo seochat tools logo