I have a .txt folder that contains files that users need to open for edit, then save.

Though I've tried fopen with 'w' I now know this to be a combination for writing over existing files in their entirety. Which can leave files totally blank.

Given this, I've tried fopen with r+ (read write ) but the file won't open this way.

So, I'm trying to enable the editing of existing files and not append them. While keeping the pointer to limit the bytes. Also, trying to make this not overwrite the existing file in event the file is called but not edited, only "looked at" and then closed. (fulfillname definitely works)

How should this be done?

if (is_writable($fullfilename)) {
if (!$fp = fopen($fullfilename, 'r+')) {
echo "Cannot open file ($fullfilename)";
exit;
}else{
while(!feof($fp)){
echo fwrite($fp,950);
}
fclose($fp);
}
?>

    <?php
    $filename = "myfile.txt";
    //check if file exists
    if (file_exists($filename)) {
    	//has the user submitted the form properly?
    	if (isset($_POST['submit']) && isset($_POST['contents'])) {
    		//can we write to the file?
    		if (is_writable($filename)) {
    			//open a file pointer to the file
    			if ($fp = fopen($filename, 'wb')) {
    				//write to the file
    				$bytes = fwrite($fp, $_POST['contents']);
    				if ($bytes === false) {
    					echo "Error: could not write to file '{$filename}'";
    				}
    				else {
    					echo "{$bytes} bytes written to file '{$filename}'";
    				}
    				fclose($fp); //now we close the file pointer
    			}
    			else {
    				echo "Error: could not open file '{$filename}' for writing";
    			}
    		}
    		else {
    			echo "Error: file '{$filename}' is not available for writing";
    		}
    	}
    	else {
    		//can we read from the file?
    		if (is_readable($filename)) {
    			echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">'
    				. '<textarea name="contents" rows="20" cols="60">'
    				//assumes PHP 4.3.0 or newer PHP version
    				. htmlspecialchars(file_get_contents($filename))
    				. '</textarea><br />'
    				. '<input type="submit" name="submit" value="Edit"></form>';
    		}
    		else {
    			echo "Error: could not open file '{$filename}' for reading";
    		}
    	}
    else {
    	echo "Error: file '{$filename}' does not exist";
    }
    ?>

      Laserlight,

      Thanks for the reply and help. I am using > 4.3

      I continue to get error messages on (3) lines up from end container, the else {
      But can't seem to figure why, tried double and single closing brackets above and below this.

      Using fullfilename (which works), I'm putting this in a textarea as follows:

                 <textarea name="textarea" cols="80" rows="15" wrap="VIRTUAL"> 
      <?php
      $filename  = $row_Recordset_ss['rID'].'__rrr.txt';
      $fullfilename=$_SERVER['DOCUMENT_ROOT']."/folder/".$filename;  
      //check if file exists if (file_exists($fullfilename)) { //user submitted the form properly? if (isset($_POST['submit']) && isset($_POST['contents'])) { //can we write to the file? if (is_writable($fullfilename)) { //open a file pointer to the file if ($fp = fopen($filename, 'wb')) { //write to the file $bytes = fwrite($fp, $_POST['contents']); if ($bytes === false) { echo "Error: could not write to file '{$fullfilename}'"; } else { echo "{$bytes} bytes written to file '{$fullfilename}'"; } fclose($fp); //now we close the file pointer } else { echo "Error: could not open file '{$fullfilename}' for writing"; } } else { echo "Error: file '{$fullfilename}' is not available for writing"; } } else { //can we read from the file? if (is_readable($fullfilename)) { echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">' . '<textarea name="contents" rows="20" cols="60">' //assumes PHP 4.3.0 or newer PHP version . htmlspecialchars(file_get_contents($fullfilename)) . '</textarea><br />' . '<input type="submit" name="submit" value="Edit"></form>'; } else { echo "Error: could not open file '{$fullfilename}' for reading"; } } //parse error here else { echo "Error: file '{$fullfilename}' does not exist"; } ?>

      Please note - below this I'm using:
      <form method="POST" name="form1" >

      <input type="submit" name="MM_update" value="Update R">
      (wondering how this integrates with your bulleted list above)

      Thankful for any help

      p.s. I tried to look up "content" in php.net searchbox before this post, no results returned.

        Write a Reply...