Jump to content


Photo

Edit Text File Record in <textarea>


  • Please log in to reply
5 replies to this topic

#1 phpnewbieca

phpnewbieca

    Advanced Member

  • Members
  • PipPipPip
  • 56 posts

Posted 02 September 2013 - 12:06 PM

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

 

http://www.dynamicdr...-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);
 }
?>

 

 



#2 kicken

kicken

    Wiser? Not exactly.

  • Gurus
  • 2,731 posts
  • LocationBonita, FL

Posted 02 September 2013 - 04:11 PM

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.
Recycle your old CD's, don't trash them!
Did I help you out?  Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

#3 phpnewbieca

phpnewbieca

    Advanced Member

  • Members
  • PipPipPip
  • 56 posts

Posted 04 September 2013 - 08:18 AM

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.



#4 phpnewbieca

phpnewbieca

    Advanced Member

  • Members
  • PipPipPip
  • 56 posts

Posted 09 September 2013 - 02:20 PM

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.";
  }
?>


#5 phpnewbieca

phpnewbieca

    Advanced Member

  • Members
  • PipPipPip
  • 56 posts

Posted 10 September 2013 - 09:03 AM

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.";
  }
?>


#6 mac_gyver

mac_gyver

    Advanced Member

  • Administrators
  • 2,611 posts

Posted 10 September 2013 - 10:23 AM

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, 10 September 2013 - 10:24 AM.

multi-purpose programming fool. well written source-code should be self-documenting. well written code should be self-troubleshooting. 





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com