the example is in Page 325, I code it and try it, every thing works good but the
displayEditForm(){ } doesn't work.
by the way this function works perfectly when the program calls it from the createFile() function, but when calls from the :
} elseif ( isset ($_GET["fileName"] ) ){
displayEditForm();
}
then it doesn't work; any body knows where is the problem?
this is the code
PHP Code:
<?php
define ( "PATH_TO_FILES", "filesTotest" );
if ( isset($_POST["saveFile"]) ) {
saveFile();
} elseif ( isset($_POST["createFile"]) ) {
createFile();
} elseif ( isset ($_GET["fileName"] ) ){
displayEditForm();
} else {
displayFileList();
}
/**************************************************************************************************/
function displayFileList ( $message=" ") { /* correct */
displayPageHeader();
if ( !file_exists(PATH_TO_FILES) ) die("Directory not Found!!!");
if ( !( $dir = dir(PATH_TO_FILES) ) ) die("Can't Open the Directory!!!");
if ($message) echo '<p class="error">' . $message . '</p>'
?>
<h2>Chose a File to Edit : </h2>
<table>
<tr>
<th>file name</th>
<th>file size</th>
<th>last change</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="textEditor1.php?filename=' . urlencode($filename) . '">' . $filename . '</a></td>';
echo '<td>' . filesize($filepath) . '</td>';
echo '<td>' . date( "M j, Y H:i:s", filemtime($filepath) ) . '</td></tr>';
}
}
$dir->close();
?>
</table>
<h2>. . . or create a new file </h2>
<form action="textEditor1.php" method="post">
<div style="width: 20em;">
<label for="filename"> Filename </label>
<div style="float: right; width: 7%; maragin-top: 0.7em;"> .txt</div>
<input type="text" name="filename" id="filename" style="width: 50%;" value="" />
<div style="clear: both;">
<input type="submit" name="createFile" value="create File" />
</div>
</div>
</form>
</body>
</html>
<?php
}
/**************************************************************************************************/
function displayEditForm($filename) {
if ( !$filename ) $filename = basename($_GET["filename"]);
if ( !$filename ) die ( " Invaled File Name !!!" );
$filepath = PATH_TO_FILES . "/$filename";
if ( !file_exists($filepath) ) die("Filed not found");
displayPageHeader();
?>
<h2> Editting . . .</h2>
<form action="textEditor1.php" method="post">
<div style="width: 40em";>
<input type="hidden" name="filename" value="<?php echo $filename ?>" />
<textarea name="fileContents" id="fileContents" rows="20" cols="80" style="width: 100%;">
<?php
echo htmlspecialchars( file_get_contents($filepath) )
?>
</textarea>
<div style="clear: both;">
<input type="submit" name="saveFile" value="Save File" />
<input type="submit" name="cancel" value=" Cancel" style="maragin-right: 20px;" />
</div>
</div>
</form>
</body>
</html>
<?php
}
/**************************************************************************************************/
function saveFile() { /* correct */
$filename = basename($_POST["filename"]);
$filepath = PATH_TO_FILES . "/$filename";
if( file_exists($filepath) ) {
if ( file_put_contents($filepath, $_POST["filecontents"]) ===false ) {
die ("Could not Save the File !!!");
displayFileList();
}
} else {
die ( "sorry the file couldn't found" );
}
}
/**************************************************************************************************/
function createFile() { /* correct */
$filename = basename($_POST["filename"]);
$filename = preg_replace("/[^A-Za-z0-9_\- ]/", "", $filename);
if ( !$filename ) {
displayFileList("Invalid File Name --- Pelase Try Again");
return;
}
$filename .= " .txt";
$filepath = PATH_TO_FILES . "/$filename";
if (file_exists($filepath) ){
displayFileList("The File is Already Exist --- Please Chose Another Name");
} else {
if ( file_put_contents($filepath, "") === false ) die ("Could not Create the New File!!!");
chmod( $filepath, 0666 );
displayEditForm("$filename");
}
}
/**************************************************************************************************/
function displayPageHeader() { ?> <!- correct -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Simple Text Editor 1</title>
<link rel="stylesheet" type="text/css" href=""common.css" />
<style type="text/css">
.error { background: #d33; color:white; padding: 0.2em; }
th { text-align:left; background-color:#999; }
th td { padding: 0.4em; }
</style>
</head>
<body>
<h1>A Simple Text Editor 1</h1>
<?php
} ?>
<!--************************************************************************************************** -->
thanks for help
