Schlo_50 Posted March 28, 2008 Share Posted March 28, 2008 I am trying to access a file and replace the eighth key within it with a new string. The structure of the file is: stringa|stringb|stringc|stringd|stringe|stringf|stringg|old one| I want to open the file, find the last key 'old one' and replace it with a new string. My code so far is: $file = file("data/customers.DAT"); foreach($file as $Key => $Val){ $Data[$Key] = explode("|", $Val); $status = trim($Data[$Key][8]); } I think I need some sort of fopen() but im not sure how to overwrite etc.. Any help appreciated! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/98305-quick-help-needed/ Share on other sites More sharing options...
rhodesa Posted March 28, 2008 Share Posted March 28, 2008 <?php $file = "data/customers.DAT"; $fh = fopen($file,'w'); foreach(file($file) as $Key => $Val){ $parts = explode("|", trim($Val)); $status = trim($parts[8]); $parts[8] = 'New String'; fwrite($fh,implode("|",$parts)."\n"); } fclose($fh); ?> Quote Link to comment https://forums.phpfreaks.com/topic/98305-quick-help-needed/#findComment-503014 Share on other sites More sharing options...
Schlo_50 Posted March 28, 2008 Author Share Posted March 28, 2008 Thank-you very much! Quote Link to comment https://forums.phpfreaks.com/topic/98305-quick-help-needed/#findComment-503015 Share on other sites More sharing options...
Schlo_50 Posted March 28, 2008 Author Share Posted March 28, 2008 Just tested that and it deleted the whole customer.DAT files contents. Anything I can try to prevent? Quote Link to comment https://forums.phpfreaks.com/topic/98305-quick-help-needed/#findComment-503017 Share on other sites More sharing options...
rhodesa Posted March 28, 2008 Share Posted March 28, 2008 Oops, sorry, need to do the file() before the fopen() <?php $file = "data/customers.DAT"; $lines = file($file); $fh = fopen($file,'w'); foreach($lines as $Key => $Val){ $parts = explode("|", trim($Val)); $status = trim($parts[8]); $parts[8] = 'New String'; fwrite($fh,implode("|",$parts)."\n"); } fclose($fh); ?> Quote Link to comment https://forums.phpfreaks.com/topic/98305-quick-help-needed/#findComment-503021 Share on other sites More sharing options...
wildteen88 Posted March 28, 2008 Share Posted March 28, 2008 I'd change rhodesa code to: $file = 'data/customers.DAT'; $new_data = ''; foreach(file($file) as $Key => $Val) { $parts = explode("|", trim($Val)); $status = trim($parts[8]); $parts[8] = 'New String'; // concate each line to $new_data string. $new_data .= implode('|', $parts) . PHP_EOL; } echo '<b>Data to be written to customers.DATA:<b><pre>' . $new_data . '</pre>'; // we can then clear the contents of the customers.DAT file and rewrite the new contents: // open the file for writing (this will clear the contents of customers.DAT) $fh = fopen($file,'w'); // write new data to customers.DAT fwrite($fh, $new_data); // close file fclose($fh); Quote Link to comment https://forums.phpfreaks.com/topic/98305-quick-help-needed/#findComment-503026 Share on other sites More sharing options...
aschk Posted March 28, 2008 Share Posted March 28, 2008 You're still using the 'w' flag to write to the file. This causes an overwrite, and I believe you should be using the 'a' flag for append instead. However you'll need to seek to the previous line before appending, and this should overwrite the last line of the file, which is, i believe, what you're wanting. You want to alter the previous recored don't you, not all of them? Quote Link to comment https://forums.phpfreaks.com/topic/98305-quick-help-needed/#findComment-503027 Share on other sites More sharing options...
rhodesa Posted March 28, 2008 Share Posted March 28, 2008 The 'w' does clear out the file, which is fine, you just need to make sure you read the current contents into a variable first. Opening it in append mode and trying to move the cursor around seems like an un-needed hassle. The second post I made should work. Oops, sorry, need to do the file() before the fopen() <?php $file = "data/customers.DAT"; $lines = file($file); $fh = fopen($file,'w'); foreach($lines as $Key => $Val){ $parts = explode("|", trim($Val)); $status = trim($parts[8]); $parts[8] = 'New String'; fwrite($fh,implode("|",$parts)."\n"); } fclose($fh); ?> Quote Link to comment https://forums.phpfreaks.com/topic/98305-quick-help-needed/#findComment-503112 Share on other sites More sharing options...
discomatt Posted March 28, 2008 Share Posted March 28, 2008 A smart way to do this is to fwrite to a new temp file. Ex file.txt.new Once the file is written successfully, rename (and overwrite) the old file to file.txt.old and the new one to file.txt. Redundancy! If anything breaks, you have a quick reference point to recover to. Repeat Quote Link to comment https://forums.phpfreaks.com/topic/98305-quick-help-needed/#findComment-503140 Share on other sites More sharing options...
rhodesa Posted March 28, 2008 Share Posted March 28, 2008 Or you can just start using MySQL Quote Link to comment https://forums.phpfreaks.com/topic/98305-quick-help-needed/#findComment-503149 Share on other sites More sharing options...
Schlo_50 Posted March 31, 2008 Author Share Posted March 31, 2008 I am using the code you posted rhodesa, but im having trouble. The code seems to remove all lines of data apart from one. I have multiple lines within the file. The code needs to search through the file for a match and then edit the last key according to the string specified. The whole code is below: $file = "data/customers.DAT"; $lines = file($file); $fh = fopen($file,'w'); foreach($lines as $Key => $Val){ $parts = explode("|", $Val); $status = $parts[8]; if ($_GET['lookup'] == $parts[0]) { $parts[8] = "".$_POST['status'].""; fwrite($fh,implode("|",$parts)."\n"); } } fclose($fh); Thanks Quote Link to comment https://forums.phpfreaks.com/topic/98305-quick-help-needed/#findComment-505477 Share on other sites More sharing options...
rhodesa Posted March 31, 2008 Share Posted March 31, 2008 That is because the had the fwrite inside the IF statement. This should work for you: <?php $file = "data/customers.DAT"; $lines = file($file); $fh = fopen($file,'w'); foreach($lines as $Key => $Val){ $parts = explode("|", $Val); $status = $parts[8]; if ($_GET['lookup'] == $parts[0]) { $parts[8] = $_POST['status']; } fwrite($fh,implode("|",$parts)."\n"); } fclose($fh); ?> Quote Link to comment https://forums.phpfreaks.com/topic/98305-quick-help-needed/#findComment-505583 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.