SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Iowa
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with a flat file admin backend

    1st problem solved, please scroll down to other posts
    firstly, i have been using little snipits of php for a long time and this is the first time i have been trying to make an admin section to a site i used, im trying to use a flat file system to do this, basically i want to use all text files as includes and go off one file, i have this working perfectly and want to edit the text files with php so updating the site is a breeze for my non-code literate friends and their websites.

    i have a form page with sections named "title" (what the name of the new file will be) and "content" (the new contents for the included text file)
    the new.php file is:
    PHP Code:
    <?php
    $newfile 
    fopen("www\MAA\$title""w");
    fwrite($newfile"$content");
    fclose($newfile);
    echo 
    "Page Created Successfully";
    ?>
    the errors i get are:
    Warning: fopen("www\MAA$title", "w") - No such file or directory in c:\appserv\www\maa\admin\new.php on line 2

    Warning: fwrite(): supplied argument is not a valid File-Handle resource in c:\appserv\www\maa\admin\new.php on line 3

    Warning: fclose(): supplied argument is not a valid File-Handle resource in c:\appserv\www\maa\admin\new.php on line 4
    any tips?
    Thanks
    Aku12
    Last edited by Aku12; Aug 14, 2003 at 04:12.

  2. #2
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Iowa
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have got this to work, and on my own even..
    this language isnt that hard after all
    Last edited by Aku12; Aug 13, 2003 at 02:31.

  3. #3
    SitePoint Addict nabeel's Avatar
    Join Date
    Nov 2002
    Location
    in westchester county, ny
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep, you should always make sure to see if the file handle returned by fopen is valid too

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,
    you escaped the $ sign (meaning the variable) with \ slash. You should do double slash \\ in "" quotes (and before the \ too)

    Quote Originally Posted by Aku12
    $newfile = fopen("www\MAA\$title", "w");
    Aku12

  5. #5
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Iowa
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually i have totally redone that, im going to post it to see if you guys have better suggestions for me:

    PHP Code:
    <?php

    //DEFINE VARIABLES

     
    $path "c:\\appserv\\www\\MAA\\";
     
    $path .= "$title";

    //NEW FILE

    if ($action == "new") {
      if(
    file_exists($path)) { 
        echo 
    "$title already Exists";
      } 
      else { 
        
    $newfile fopen("$path""w");
        
    fwrite($newfile"$content");
        
    fclose($newfile);
        echo 
    "$title Created Successfully";
      }
    }

    //EDIT FILE

    elseif ($action == "edit") {
      
    $newfile fopen("$path""w");
      
    fwrite($newfile"$content");
      
    fclose($newfile);
      echo 
    "$title Updated Successfully";
    }

    //DELETE FILE

    elseif ($action == "delete") {
      if(
    file_exists($path)) { 
        
    unlink ($path);
        echo 
    "$title Deleted Successfully";
      } 
      else { 
        echo 
    "$title does not Exist";
      }
    }
    ?>
    i have all of them working perfectly in seperate files, but now that i put them all in the same i am having a problem
    if the variables are defined in the url (do.php?action=edit&title=Gallery.txt) it works as should
    but when coming from a form that defines the $title variable it doesn't work,
    i printed out the $path at the beginning to make sure that the $title was getting sent and it was,
    is this the best way to make them all into one file or should i change it from the if and elseif statements?


    that is how i have those done, so i mostly have the functions of the admin panel done, now im trying to get the main part of it organized and easy to use, so far i have it so that php spits out all the files in the directory in a while loop, i need to have it so that it only spits out the .txt files and if it is header.txt index.txt or footer.txt to spit them out first and with different features (basically just wont have the delete button)
    and this is how i did that, any help on this would be appreciated
    PHP Code:

    <?php
     $path 
    "c:\\appserv\\www\\MAA\\";
     
    $title .= ".txt";
     
    $path .= "$title";
    $dir "c:\\appserv\\www\\MAA\\";
             echo 
    "<table>";
    if (
    is_dir($dir)) {
        if (
    $dh opendir($dir)) {
            while ((
    $file readdir($dh)) !== false) {
       if ((
    $file == "index.txt") || ($page == "header.txt") || ($page == "footer.txt")) {  
             echo 
    "<tr><td>filename: $file <td> <a href=\"edit.php?title=$file\">EDIT</a> <td> no delete</tr> ";
    }
       else if (
    $file == $file) {
             echo 
    "<tr><td>filename: $file <td> <a href=\"edit.php?title=$file\">EDIT</a> <td><a href=\"do.php?action=delete&title=$file\">DELETE</a></tr> ";
    }
            }
        
    closedir($dh);
        }
    }
    ?>
    i tried setting if $file == "*.txt" to output only the text files and not the php files or directory but that doesnt allow any to be displayed, is there away to make it only display the .txt files?
    thanks
    aku12
    Last edited by Aku12; Aug 14, 2003 at 04:09.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,
    you can't just set the if ( $file == "*.txt" ) check as you would do in command line, you should use a regular expression like
    if ( preg_match("/\.txt$/", $file) )
    have fun!

  7. #7
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Iowa
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    awesome that is exactly what i needed, thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •