p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Problems with example code in php 4 chapter 10


Message #1 by "Travis Workman" <socraties@s...> on Tue, 13 Feb 2001 00:08:42
I am new to PHP, and I haven't found it that hard, but in the PHP4 book

(PHP 4 Beginning Programming) in chapter 10, I am having several different 

problems getting the examples to work. I am running IIS on win 2000. It is 

the editor.php example page353-358. I get 3 errors upon trying to use the 

script, they all occur in the first function... function editor_form. 

First Error is: Warning: Missing argument 3 for editor_form() in 

C:\Inetpub\wwwroot\Socraties\editor.php on line 4



Second Error is:Warning: file("") - No such file or directory in 

C:\Inetpub\wwwroot\Socraties\editor.php on line 8



Third Error is:Warning: Bad arguments to implode() in 

C:\Inetpub\wwwroot\Socraties\editor.php on line 8



if you would like to look at the code you can see it at. 

http://24.13.131.128/Socraties/editor.inc. I named it .inc so the code 

would immediately show up in the browser. Any help on this would be 

greatly apperciated. 

Thanks, 

Travis Workman

Message #2 by "Prock" <peet4130@y...> on Wed, 28 Feb 2001 20:38:57
I had the same problem. I looked at the erata and fixed a couple of things 

but it still didn't work. There were some other bugs in the code.



The missing argument error happens because when the scipt calls this 

function it says:



editor_form ("$dir/$filename", $is_new);



which are only two arguments. The correct code should be:



editor_form ($dir, $filename, $is_new);





the other bug is the swapping line 7 and 8.



$filepath = "$dir/$filename";

if(!$is_new) $filebody = $implode("",file($filepath));





Here's the complete code just in case



<?php

//editor.php

include "./common.inc";



function editor_form($dir, $filename, $is_new) {

   global $PHP_SELF, $edit_form_cols, $edit_form_rows;

   $filepath = "$dir/$filename";

if(!$is_new) $filebody = implode("",file("$filepath"));

   $file_info_array = file_info("$filepath");

   $editable = 1;   

   if($file_info_array["filetype"] != "text") {

      $filebody = $filepath . " is not a text file.

                     You had better not edit it.";

      $editable = 0;

   }

   if($editable) {

?>

<CENTER>

<FORM NAME="edit_form" METHOD="POST" action="<?php echo $PHP_SELF ?>">

<INPUT TYPE="HIDDEN" NAME="action" VALUE="save_file">

<INPUT TYPE="HIDDEN" NAME="dir" VALUE="<?php echo "$dir" ?>">

<TEXTAREA ROWS="<?php echo $edit_form_rows ?>" NAME="filebody"

      COLS="<?php echo $edit_form_cols ?>" WRAP="soft">

<?php echo "$filebody"; ?>

</TEXTAREA><BR>

Filename: <?php echo "<STRONG>$dir/</STRONG>"; ?>

<INPUT TYPE="TEXT" NAME="filename" VALUE="<?php echo $filename ?>"

                              SIZE="30">

<INPUT TYPE="SUBMIT" VALUE="Save" NAME="Submit">

</FORM>

</CENTER>



<?php

   }

   else {

      echo "<CENTER><STRONG><FONT 

COLOR=\"RED\">$filebody</FONT></STRONG></CENTER>\n";

   }

}



function edit_new_form() {

   global $PHP_SELF, $default_dir, $dir;

?>



<CENTER><FORM METHOD="POST" ACTION="<?php echo $PHP_SELF ?>">

<INPUT TYPE="HIDDEN" NAME="action" VALUE="editor_page">

<INPUT TYPE="HIDDEN" NAME="dir" VALUE="<?php echo $dir ?>">

<INPUT TYPE="SUBMIT" VALUE="Edit New"></FORM></CENTER>



<?php

}



function save_file() {

   global $filename, $filebody, $dir, $PHP_SELF;

   if(file_exists("$dir/$filename")) {

      echo "<SCRIPT>result 

         confirm(\"Overwrite '$dir/$filename'?\");

         if(!result) history.go(-1);</SCRIPT>";

   }

   if($file = fopen("$dir/$filename", "w")) {

      fputs($file, $filebody);

      fclose($file);

   }

   else {

      error_message("Can't save file $dir/$filename.");

   }

   echo "<SCRIPT>self.location.href='$PHP_SELF?dir=$dir&

                                            filename=$filename';</SCRIPT>";

}



function editor_page() {

   global $dir, $filename, $default_filename;

   $is_new = 0;

   

   if($filename == '') {

      $filename = $default_filename;

      $is_new = 1;

   }

   if(!file_exists("$dir/$filename")) $is_new = 1;

   if(!$is_new) {

   edit_new_form();

   ?>

   <TABLE BORDER="1" WIDTH="100%">

   <TR><TH WIDTH="100%" COLSPAN="2">

   <CENTER><STRONG>Stats for <?php echo "$dir/$filename" ?>

   </TD></TR>



   <?php

   $file_info_array = file_info("$dir/$filename");

   foreach($file_info_array as $key=>$val) {

      echo "<TR><TH WIDTH=\"30%\">". ucfirst($key) .

           "</TD><TD WIDTH=\"70%\">" . $val . 

           "</TD></TR>\n"; 

   }

?>

</TABLE>

<?php

   } else {

      echo "<CENTER><STRONG>Editing a new file</STRONG></CENTER>\n";

   }

   editor_form($dir,$filename, $is_new);

}



if(empty($dir) || !ereg($default_dir, $dir)) {

   $dir = $default_dir;

}

switch ($action) {

   case "save_file":

      save_file();

      break;   

   default:

      html_header();

      editor_page();

      html_footer();

      break;

}

?>






  Return to Index