Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > BOOK: Beginning PHP 5.3
|
BOOK: Beginning PHP 5.3
This is the forum to discuss the Wrox book Beginning PHP 5.3 by Matt Doyle; ISBN: 978-0-470-41396-8
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP 5.3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 2nd, 2011, 02:19 AM
Authorized User
 
Join Date: Jun 2011
Posts: 51
Thanks: 9
Thanked 0 Times in 0 Posts
Send a message via MSN to nawar youssef
Default Hey, anybody still come here, can not find real help!!!

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 htmlspecialcharsfile_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$filepath0666 );
        
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
 
Old October 3rd, 2011, 06:52 AM
Friend of Wrox
 
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

Change this:
Code:
} elseif ( isset ($_GET["fileName"] ) ){
to this:
Code:
} elseif ( isset ($_GET["filename"] ) ){
The Following User Says Thank You to UseLess For This Useful Post:
nawar youssef (October 3rd, 2011)
 
Old October 3rd, 2011, 12:00 PM
Authorized User
 
Join Date: Jun 2011
Posts: 51
Thanks: 9
Thanked 0 Times in 0 Posts
Send a message via MSN to nawar youssef
Default O M G thank you so much

 
Old October 4th, 2011, 11:23 AM
Registered User
 
Join Date: Aug 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Great Find

Great Find Useless.
 
Old July 12th, 2012, 02:46 AM
Registered User
 
Join Date: Jul 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Spot the difference?

Just new to PHP - what exactly was the difference between the wrong and right code snippets? Yes, embarrassing question...
 
Old July 12th, 2012, 06:29 AM
Friend of Wrox
 
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

In the wrong snippet the;
Code:
$_GET["fileName"]
contains a capital N when it should read;
Code:
$_GET["filename"]
You can see how array keys work when using strings in the following example;
Code:
<?php
/*
* Array keys example
*/

$example = array();

$example['filename'] = 'filename';
$example['fileName'] = 'filename';
$example['Filename'] = 'filename';
$example['FileName'] = 'filename';
$example['FILENAME'] = 'filename';

echo '<pre>';
print_r($example);
echo '</pre>';

?>
You should get an array with 5 elements, they all have the same value but the keys are different.
 
Old July 12th, 2012, 06:39 AM
Registered User
 
Join Date: Jul 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default How embarrassing...

I spent 5 minutes trying to find the difference between the two. How in God's green earth did I miss that...?

Thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
Hey Hal ! augustine SQL Server 2000 1 August 2nd, 2004 08:57 AM
Hey where is the perl beginning page. carlos123 All Other Wrox Books 0 January 5th, 2004 05:10 PM
Hey Wrox Service Team...Help and ? kamarkh BOOK: ASP.NET Website Programming Problem-Design-Solution 1 October 20th, 2003 10:00 PM
Hey, JavaScript guru lcsgeek Classic ASP Databases 2 September 11th, 2003 08:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.