Hey Everyone,

Quick question. I have been using FCKeditor (http://www.fckeditor.net ) and I am trying to set it up so that when I go into the administration, I can type in anything I need to, and have it save to a txt file. However, our server is only running PHP 4.3.1 and it does not support some functions. All the permissions are set and stuff, so I don't understand why its not working. I am making a CMS, that will not require a MySQL database.. so yeah, thats why im not doing it that way... (I hope somebody will be able to help) I have pasted my code below:

<?php include('../includes/header.php')?>

<?php include('../includes/sidebar.php')?>


<td id="main_content">

  <span class="heading_text">TxTCmS Administration</span><br />    <br />
  <?php
include("../FCKeditor/fckeditor.php") ;
?>

<?php
$File = "../txt/grad.txt";
$Handle = fopen($File, 'w');
$Data = $body;
fwrite($Handle, $Data);
$Data = "$body";
fwrite($Handle, $Data);
fclose($Handle);
?>

<form method="post" action"fwrite">

<?php $oFCKeditor = new FCKeditor('FCKeditor1') ;
$oFCKeditor->BasePath = '../FCKeditor/';
$oFCKeditor->Height = '350' ;
$oFCKeditor->Value = $body;
$oFCKeditor->Create() ;

?>


<input type="submit" name="submit" value="Save Page"></input>

</form></td>

</table>

<?php include('../includes/footer.php')?>

    You need to set the directory (txt) persmissions to allow write to file and allow recursive permissions. CHMOD 0777

      Frederick wrote:

      You need to set the directory (txt) persmissions to allow write to file and allow recursive permissions. CHMOD 0777

      yeah. i have done that... however, I think it has somthing to do with the variables.... a the moment, it reads the file okay, but it doesnt writes a blank message back to the txt file.... I dont understand why it is doing that..

        Hey.. I just changed my code... and... it still doesnt work.. 🙁

        Here is the updated version...

        <?php include('../includes/header.php')?>
        
        <?php include('../includes/sidebar.php')?>
        
        
        <td id="main_content">
        
        <?php
        $filename = '../txt/grad.txt';
        
        // Let's make sure the file exists and is writable first.
        if (is_writable($filename)) {
        
           // In our example we're opening $filename in append mode.
           // The file pointer is at the bottom of the file hence
           // that's where $somecontent will go when we fwrite() it.
           if (!$handle = fopen($filename, 'a')) {
                 echo "Cannot open file ($filename)";
                 exit;
           }
        
           // Write $somecontent to our opened file.
           if (fwrite($handle, $somecontent) === TRUE) {
               echo "Cannot write to file ($filename)";
               exit;
           }
        
           echo "Success, wrote ($somecontent) to file ($filename)";
        
           fclose($handle);
        
        } else {
           echo "The file $filename is not writable";
        }
        ?> 
        
          <span class="heading_text">TxTCmS Administration</span><br />    <br />
          <?php
        include("../FCKeditor/fckeditor.php") ;
        ?>
        
        <form method="post" action"<?php $handle ?>">
        
        <?php $oFCKeditor = new FCKeditor('FCKeditor1') ;
        $oFCKeditor->BasePath = '../FCKeditor/';
        $oFCKeditor->Height = '350' ;
        $oFCKeditor->Value = $somecontent;
        $oFCKeditor->Create() ;
        
        ?>
        
        
        <input type="submit" name="submit" value="Save Page"></input>
        
        </form></td>
        
        </table>
        
        <?php include('../includes/footer.php')?>

        I got this from http://www.php.net , however its so wierd. you need to know tons of functions to be able to figure it out.

        Any suggestions on what to do? or can someone try fixing my code?

          Have you tried running the code in a separate file to see if it succeeds.

          <?php
          // call page test.php
          $File = "../txt/grad.txt"; 
          $Handle = fopen($File, 'w+'); 
          $Data = "Use no code input, just straight text here"; 
          fwrite($Handle, $Data); 
          fclose($Handle); 
          ?>

          After running the test page, see if the file exists and what its contents are. When I do write to file, I always do it in a process method, not within a viewable page content.

            okay. we are further now. That seems to add it to the txt file, rather than overwrite the txt file. However i have it setup with any variable, and just straight text.

            Once i get it hooked up to the other php file i am going to try and use a variable for it and see if it adds.

            However, any clue on how to overwrite it, rather than add to it?

              Frederick wrote:

              Just remove the + in the w+

              Okay.. Now.. how can I get it so it writes from my first file??

                Uh.. Where do I start? 🙂

                Okay, first one:

                <form method="post" action"<?php $handle ?>"> 
                

                <?php $handle ?> doenst do anything(you dont print it). This translates to:
                <form method="post" action""> and you are missing "="-sign after action.

                Action should be the scriptname where you want to send the information to be processed. I think you tried to send it to $handle which is filehandle for your textfile.

                In this case you probably want to send it to the same script where you are sending from:

                <form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
                

                There was so much missing and Im more quicker to code than explain (and I'm tired) so heres(hopefully) the working script..

                Before the script heres a few tips:

                • Learn how to process forms with PHP. Study $GET, $POST and other predefined variables.

                • Do the form processing at the beginning of the script.

                <?php
                $filename = '../txt/grad.txt';
                $msg = '';
                
                if (isset($_POST['FCKeditor1'])) {
                	// took out the is_writable bcos the next check will fail if it isnt
                	if (!$handle = fopen($filename, 'w')) {
                		$msg = "Cannot open file ($filename)";
                	} else {
                		if (fwrite($handle, $_POST['FCKeditor1']) === TRUE) {
                			$msg = "Cannot write to file ($filename)";
                		} else {
                			$msg = 'Wrote syccesfully to file ('.$filename.')';
                			$content = $_POST['FCKeditor1'];
                			fclose($handle);
                		}
                	}
                }
                
                // If $content is not yet set and $filename exists, retrieve the contents of the file
                if (!isset($content) && file_exists($filename)) {
                	$content = file_get_contents($filename);
                } else {
                	$content = '';
                }
                
                include('../includes/header.php');
                include('../includes/sidebar.php');
                
                
                <td id="main_content">
                  <span class="heading_text">TxTCmS Administration</span><br />    <br />
                <?php 
                // Print status message if there is any.
                if (!empty($msg)) {
                	echo '<strong>'.$msg.'</strong';
                }
                
                include("../FCKeditor/fckeditor.php"); ?>
                
                <form method="post" action"<?php echo $_SERVER['SCRIPT_NAME'];?>">
                
                <?php 
                $oFCKeditor = new FCKeditor('FCKeditor1') ;
                $oFCKeditor->BasePath = '../FCKeditor/';
                $oFCKeditor->Height = '350' ;
                $oFCKeditor->Value = $content;
                $oFCKeditor->Create() ;
                ?>
                
                <input type="submit" name="submit" value="Save Page"></input>
                </form></td>
                </table>
                
                <?php include('../includes/footer.php')?> 
                

                  Awesome! Thank You! That Worked. However, you forgot 1 closing php statment

                  ?>

                  so for anyone else who is trying this you can use

                  <?php include('../includes/header.php')?>
                  
                  <?php include('../includes/sidebar.php')?>
                  
                  
                  <td id="main_content">
                  
                  
                  
                    <span class="heading_text">TxTCmS Administration</span><br />    <br />
                    <?php
                  include("../FCKeditor/fckeditor.php") ;
                  ?>
                  
                  <?php
                  $body = implode('', file('../txt/grad.txt'));
                  ?>
                  
                  <form method="post" action="saveData.php">
                  
                  <?php $oFCKeditor = new FCKeditor('FCKeditor1') ;
                  $oFCKeditor->BasePath = '../FCKeditor/';
                  $oFCKeditor->Height = '350' ;
                  $oFCKeditor->Value = $body;
                  $oFCKeditor->Create() ;
                  ?>
                  
                  <input type="submit" name="submit" value="Save Page"></input>
                  
                  </form></td>
                  
                  </table>
                  
                  <?php include('../includes/footer.php')?>

                  However remember to take out my Include statements, for my header, sidebar, and footer.

                  THANK YOU SO MUCH FOR HELPING!!!

                    Yeah I forgot a closing statement. Its 5:35 am in the morning 🙂

                    Why are you doing this:

                    $body = implode('', file('../txt/grad.txt')); 
                    

                    ..instead of this:

                    $body = file_get_contents('../txt/grad.txt');
                    

                    file_get_contents() will give you the contents as a string. file() gives an array (separate lines) and you dont need that(thats why you are joining them with implode() to get a string).

                      cahva wrote:

                      Yeah I forgot a closing statement. Its 5:35 am in the morning 🙂

                      Why are you doing this:

                      $body = implode('', file('../txt/grad.txt')); 
                      

                      ..instead of this:

                      $body = file_get_contents('../txt/grad.txt');
                      

                      file_get_contents() will give you the contents as a string. file() gives an array (separate lines) and you dont need that(thats why you are joining them with implode() to get a string).

                      Oh! The reason why I am doign that is because, i am using and older version of PHP. Our server, is managed by someone else, and that person will not let me lay a finger on the server, to upgrade it.... unfortuantly...

                        MillerTech wrote:

                        Oh! The reason why I am doign that is because, i am using and older version of PHP. Our server, is managed by someone else, and that person will not let me lay a finger on the server, to upgrade it.... unfortuantly...

                        Hmm.. It should work still if the version is 4.3.1. file_get_contents is available from 4.3.0.
                        Theres really no reason not to upgrade the version to latest 4.x series. Theres no major changes in functions, just bug and security fixes.

                          one last little problem. Whenever i stick in a hyperlink or an image or somthing it always puts a / either in the " at both the beginning and the end.

                          It outputs like this:

                          <a href=/"emailmehere@email.com/">Name Here</a>

                          The code is fine before i save it.

                          I know that i need to use the stripslashes function, however I am not too sure on where to put it.

                          Any thoughts?

                            You may need to use stripslashes() to get rid of those slashes. It happens when magic quotes are on. Read the manual about it.

                              4 days later
                              cahva wrote:

                              You may need to use stripslashes() to get rid of those slashes. It happens when magic quotes are on. Read the manual about it.

                              I have been playing with this for the past couple days.. and can't get anything.. any suggestions...

                                11 days later

                                Here is the script code you graciously shared.

                                cahva wrote:
                                <?php
                                $filename = '../txt/grad.txt';
                                $msg = '';
                                
                                if (isset($_POST['FCKeditor1'])) {
                                	// took out the is_writable bcos the next check will fail if it isnt
                                	if (!$handle = fopen($filename, 'w')) {
                                		$msg = "Cannot open file ($filename)";
                                	} else {
                                		if (fwrite($handle, $_POST['FCKeditor1']) === TRUE) {
                                			$msg = "Cannot write to file ($filename)";
                                		} else {
                                			$msg = 'Wrote syccesfully to file ('.$filename.')';
                                			$content = $_POST['FCKeditor1'];
                                			fclose($handle);
                                		}
                                	}
                                }
                                
                                // If $content is not yet set and $filename exists, retrieve the contents of the file
                                if (!isset($content) && file_exists($filename)) {
                                	$content = file_get_contents($filename);
                                } else {
                                	$content = '';
                                }
                                
                                include('../includes/header.php');
                                include('../includes/sidebar.php');
                                
                                
                                <td id="main_content">
                                  <span class="heading_text">TxTCmS Administration</span><br />    <br />
                                <?php 
                                // Print status message if there is any.
                                if (!empty($msg)) {
                                	echo '<strong>'.$msg.'</strong';
                                }
                                
                                include("../FCKeditor/fckeditor.php"); ?>
                                
                                <form method="post" action"<?php echo $_SERVER['SCRIPT_NAME'];?>">
                                
                                <?php 
                                $oFCKeditor = new FCKeditor('FCKeditor1') ;
                                $oFCKeditor->BasePath = '../FCKeditor/';
                                $oFCKeditor->Height = '350' ;
                                $oFCKeditor->Value = $content;
                                $oFCKeditor->Create() ;
                                ?>
                                
                                <input type="submit" name="submit" value="Save Page"></input>
                                </form></td>
                                </table>
                                
                                <?php include('../includes/footer.php')?> 
                                

                                I made one or two corrections. And configured it for my FCK install/needs.

                                However, the one thing it does not seem to do is reload $content. After making edits and posting the changes, the target $filename is modified. The page reloads itself, but this time $content is blank.

                                Why? I had expected it to retrieve the $filename, in order to provide visual proof the edits occurred (in addition to the message line that gets inserted).

                                Indeed the code has a line to check if $filename exists and, if so, load it as $content. Yet, it does not.

                                Thanks for any help on this point.

                                  LearningPHP wrote:

                                  Here is the script code you graciously shared.

                                  I made one or two corrections. And configured it for my FCK install/needs.

                                  However, the one thing it does not seem to do is reload $content. After making edits and posting the changes, the target $filename is modified. The page reloads itself, but this time $content is blank.

                                  Why? I had expected it to retrieve the $filename, in order to provide visual proof the edits occurred (in addition to the message line that gets inserted).

                                  Indeed the code has a line to check if $filename exists and, if so, load it as $content. Yet, it does not.

                                  Thanks for any help on this point.

                                  Now When you make some kind of link, does it act funny? does it put some kind of slashes in, that you need to add

                                  stripslashes()

                                    Nope, I have no problem with slashes being introduced. So I haven't had to apply the stripslashes function anywhere.

                                    My problem is that I after posting the form, the page reloads but without including the (recently changed) target tile. Why?

                                      Write a Reply...