Page 363 - Beginning PHP 5.3
P. 363

Chapter 11: Working with Files and Directories
                           Building a Text Editor

                           With the basics of PHP ’ s file and directory handling capabilities under your belt, it ’ s time to create a
                         simple Web - based text file editor application. The editor will display a list of text files in a designated
                         folder, inviting the user to edit a file by clicking its name. The edit page will simply display the file ’ s
                         contents in an HTML text area field, with buttons for saving changes or canceling edits.

                           The user will also be able to create new text files to work with. For the sake of simplicity the editor will
                         only handle text files with the   .txt  filename extension.


                           The Text Editor Script

                          Here ’ s the code for the text editor. Save it as  text_editor.php  in your document root folder:
                               < ?php

                             define( “PATH_TO_FILES”, “/home/matt/sandbox” );

                             if ( isset( $_POST[“saveFile”] ) ) {
                               saveFile();
                             } elseif ( isset( $_GET[“filename”] ) ) {
                               displayEditForm();
                             } elseif ( isset( $_POST[“createFile”] ) ) {
                               createFile();
                             } else {
                               displayFileList();
                             }

                             function displayFileList( $message=”” ) {
                               displayPageHeader();
                               if ( !file_exists( PATH_TO_FILES ) ) die( “Directory not found” );
                               if ( !( $dir = dir( PATH_TO_FILES ) ) ) die( “Can’t open directory” );

                             ? >
                                  < ?php if ( $message ) echo ‘ < p class=”error” > ’ . $message . ‘ < /p > ’ ? >
                                  < h2 > Choose a file to edit: < /h2 >
                                  < table cellspacing=”0” border=”0” style=”width: 40em; border: 1px solid
                             #666;” >
                                    < tr >
                                      < th > Filename < /th >
                                      < th > Size (bytes) < /th >
                                      < th > Last Modified < /th >
                                    < /tr >
                               < ?php

                               while ( $filename = $dir- > read() ) {
                                 $filepath = PATH_TO_FILES . “/$filename”;
                                 if ( $filename != “.”  & &  $filename != “..”  & &  !is_dir( $filepath )  & &




                             strrchr( $filename, “.” ) == “.txt” ) {

                                    echo ‘ < tr > < td > < a href=”text_editor.php?filename=’ . urlencode(


                             $filename ) . ‘” > ’ . $filename . ‘ < /a  > < /td > ’;
                                    echo ‘ < td > ’ . filesize( $filepath ) . ‘ < /td > ’;
                                                                                                         325



                                                                                                      9/21/09   9:10:21 AM
          c11.indd   325
          c11.indd   325                                                                              9/21/09   9:10:21 AM
   358   359   360   361   362   363   364   365   366   367   368