Jump to content

Edit Text File Record in <textarea>


phpnewbieca

Recommended Posts

I know you can edit a text file in a <textarea> with the following code I got from:

 

http://www.dynamicdrive.com/forums/showthread.php?4539-how-do-i-modify-existing-txt-files-with-php

 

 

 
<? 
 if($_POST['Submit']){ 
  $open = fopen("textfile.txt","w+"); 
  $text = $_POST['update']; 
  fwrite($open, $text); 
  fclose($open); 
  echo "File updated.<br />";  
  echo "File:<br />"; 
  $file = file("textfile.txt"); 
  foreach($file as $text) { 
   echo $text."<br />"; 
  } 
 }
 else{ 
  $file = file("textfile.txt"); 
  echo "<form action=\"".$PHP_SELF."\" method=\"post\">"; 
  echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">"; 
  foreach($file as $text) { 
   echo $text; 
  }  
  echo "</textarea>"; 
  echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n 
  </form>";   
 } 
?>

 

I'm thinking is it possible to edit a specific record in a text file in a <textarea>.

 

 

 

<? 
 //### Process edit_textform
 error_reporting(-1);
 //### Define Variable
 $filename = "repair_upgrade.txt";
 $path = $_SERVER['DOCUMENT_ROOT']."/home/users/web/b686/dom.horacela/public_html/";
 $searchfor = preg_replace('#[\D]#', '', $confirmation_number);
 //### Get Data From Form
 $confirmation_number = isset($_POST['confirmation_number']) ? $_POST['confirmation_number'] : FALSE ;
 //### Validate confirmation number
 if(empty($confirmation_number)) {
  echo 'No confirmation number.'. PHP_EOL;
  exit;
 } 
 else {
  //### Remove anything that is a not a number
  $confirmation_number = preg_replace('#[\D]#', '', $confirmation_number);
 }
 //### Check if file is writable
 if(!is_writable($filename)) {
  echo 'The file is not writable.'. PHP_EOL;
  exit;
 }
 //### Check if file is readable
 if (!is_readable($filename)) {
  echo 'The file is not readable'. PHP_EOL;
  exit;
 }  
 elseif($searchfor) != FALSE) {
  //### Search file for Confirmation_number. 
  $file = file($filename);  
  //### Set pointer to current record
  fseek($file, SEEK_CUR);
  echo "<form action=\"".$PHP_SELF."\" method=\"post\">"; 
  echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">"; 
  While(!preg_match('/\bEND\b/i',$file)) {
   foreach($file as $text) { 
    echo $text; 
   }
  }   
  echo " </textarea><br />\n";
  echo " <input name=\"Submit\" type=\"submit\" value=\"Update\" />\n </form>\n"; 
 }
 else {
  if($_POST['Submit']){ 
   $open = fopen($filename,"a+"); 
   $text = $_POST['update']; 
   fwrite($open, $text); 
   fclose($open); 
   echo "File updated.<br />";  
   echo "File: $filename <br />";
  } 
  $file = file($filename); 
  foreach($file as $text) { 
   echo $text."<br />"; 
  }
  fclose($fh);
 }
?>

 

 

Link to comment
Share on other sites

You need a way to extract and replace specific records from the file. Exactly how that would be done depends on the format of the file in question. If it's as simple as one record per line, you could just use file to break the file into records, and extract the correct one from the array for editing. To save you'd split the file into records again, replace the correct array entry, then re-write the array to the file.

 

 

Once you have two functions created, one allowing you to extract a specific record, and one to replace a specific record, building the form and PHP to handle it is easy.

Link to comment
Share on other sites

You need a way to extract and replace specific records from the file. Exactly how that would be done depends on the format of the file in question. If it's as simple as one record per line, you could just use file to break the file into records, and extract the correct one from the array for editing. To save you'd split the file into records again, replace the correct array entry, then re-write the array to the file.

 

 

Once you have two functions created, one allowing you to extract a specific record, and one to replace a specific record, building the form and PHP to handle it is easy.

Thanks for responding, I don't think the end result is worth the effort.

Link to comment
Share on other sites

I am trying ti edit a record in a text file.

The problem is I cannot find the $confirmation_number:

This line =>   if(strpos("Confirmarion Number:".$confirmation_number, $array) !== FALSE) {

 

 

<?php 
 //### Process edit_textform
 error_reporting(-1);
 //### Define Variable
 //### Get Data From Form
 $filename = isset($_POST['file_name']) ? $_POST['file_name'] : FALSE ;
 //### Validate filename
 if(empty($filename)) {
  echo "You did not enter a file name i.e. myfile.txt.". PHP_EOL;
  exit;
 } else {
    echo "";
 } 
 $confirmation_number = isset($_POST['confirmation_number']) ? $_POST['confirmation_number'] : FALSE ;
 //### Validate confirmation number
 if(empty($confirmation_number)) {
  echo 'No confirmation number.'. PHP_EOL;
  exit;
 } 
 else {
  //### Remove anything that is a not a number
  $searchfor = preg_replace('#[\D]#', '', $confirmation_number);
 }
 ///### check if file exist & is readable
 if (!file_exists($filename)) {
    echo 'The file does not exist'. PHP_EOL;
    exit;
  } 
 if (!is_readable($filename)) {
    echo 'The file is not readable'. PHP_EOL;
    exit;
  }   
 ///### Put Text File into Array
 $array = file($filename);
 $found = false;
 //### Cant fint $confirmation Number
 *** This Line => if(strpos("Confirmarion Number:".$confirmation_number, $array) !== FALSE) {
  $found = true; 
  echo "<form action=\"".$PHP_SELF."\" method=\"post\">"; 
  echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";   
  while($line = fgets($array, 1000)) {
   foreach($line as $text) { 
    echo $text; 
   }  
   //echo $line;
   //continue;
  }  
  echo "</textarea>"; 
  echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n 
  </form>";    
 }
 else {
 echo "Confirmation number not found.";
  }
?>
Link to comment
Share on other sites

Still can't find confirmation number, line 41 -- No errors

 

 

<?php 
 //### Process edit_textform
 error_reporting(-1);
 //### Define Variable
 //$filename = "repair_upgrade.txt";
 //### Get Data From Form
 $filename = isset($_POST['file_name']) ? $_POST['file_name'] : FALSE ;
 //### Validate filename
 if(empty($filename)) {
  echo "You did not enter a file name i.e. myfile.txt.". PHP_EOL;
  exit;
 } else {
    echo "";
 } 
 $confirmation_number = isset($_POST['confirmation_number']) ? $_POST['confirmation_number'] : FALSE ;
 //### Validate confirmation number
 if(empty($confirmation_number)) {
  echo 'No confirmation number.'. PHP_EOL;
  exit;
 } 
 else {
  //### Remove anything that is a not a number
  $searchfor = preg_replace('#[\D]#', '', $confirmation_number);
 }
 ///### check if file exist & is readable
 if (!file_exists($filename)) {
    echo 'The file does not exist'. PHP_EOL;
    exit;
  } 
 if (!is_readable($filename)) {
    echo 'The file is not readable'. PHP_EOL;
    exit; 
  }   
 //Define Variable(s) 
 $found = false; 
 ///### Put Text File into Array
 $array = file($filename);
 ///### Rewind array to first element
 reset($array);
**** //### Search for string
 if(in_array($searchfor, $array)){
  $found = true; 
  echo "<form action=\"".$PHP_SELF."\" method=\"post\">"; 
  echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";   
  while($array = fgets($line, 1000)) {
   foreach($line as $text) { 
    echo $text; 
   }  
  }  
  echo "</textarea>"; 
  echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n 
  </form>";    
 print "<br/><br/>";
 //### Put text file in Array
 $array = file($filename);
 foreach($array as $line){
  $line = rtrim($line);
  print $line . "<br/>";
 }  
} 
 else {
  echo "Confirmation number not found.";
  }
?>
Link to comment
Share on other sites

what does a sample of your data look like? without knowing what you are trying to find, baring an obvious error in your logic, we cannot possibly help you with the problem.

 


 

btw - the problems you are having doing this (writing a bunch of code to accomplish finding a value) is why data is usually stored in a database and all you have to do is write and execute a query statement to find it.

Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.