voyde Posted April 16, 2008 Share Posted April 16, 2008 Ok I need a flat file to hold one variable... ten different times. A user submits let say their name, the script adds the users name into the flat file erasing the one at the bottom (so there is only 10 at a time). What i got... $filename = $configpath.'lastten.txt'; if (!$handle = fopen($filename, 'r+')) { $error = "Cannot open file ($filename)"; exit; } if (fwrite($handle, $direct_link) === FALSE) { $error = "Cannot write to file ($filename)"; exit; } fclose($handle); How on earth do i get it to write the latest at the top and remove the what once was number 10. Link to comment https://forums.phpfreaks.com/topic/101335-flat-file-question/ Share on other sites More sharing options...
Cep Posted April 16, 2008 Share Posted April 16, 2008 Are you using PHP4 or 5? It would probably be easier to read in the original ten lines, store that as an array, edit the last index and then overwrite the original file with a new one. Would need to know what version your using though. Link to comment https://forums.phpfreaks.com/topic/101335-flat-file-question/#findComment-518310 Share on other sites More sharing options...
voyde Posted April 16, 2008 Author Share Posted April 16, 2008 Php 5 Link to comment https://forums.phpfreaks.com/topic/101335-flat-file-question/#findComment-518781 Share on other sites More sharing options...
Cep Posted April 16, 2008 Share Posted April 16, 2008 Try something like this, <?php $filename = $configpath.'lastten.txt'; // Read in file if ($details = file($filename)===false) { $error = "Cannot open file {$filename}"; exit; } else // Change last index $details[9] = "My new string\n"; if ($result = file_put_contents($filename,$details)===false) { $error = "Cannot write to file ($filename)"; exit; } ?> I am not at a server to test this but using these two functions better then using the fopen methods. Link to comment https://forums.phpfreaks.com/topic/101335-flat-file-question/#findComment-518806 Share on other sites More sharing options...
voyde Posted April 16, 2008 Author Share Posted April 16, 2008 That doesnt work it just keeps making one line with the most recent data. Link to comment https://forums.phpfreaks.com/topic/101335-flat-file-question/#findComment-518835 Share on other sites More sharing options...
BlueSkyIS Posted April 16, 2008 Share Posted April 16, 2008 works for me: <?php $filename = $configpath.'lastten.txt'; // Read in file $details = file($filename); // Change last index $details[9] = "My new string\n"; if ($result = file_put_contents($filename,$details)===false) { $error = "Cannot write to file ($filename)"; exit; } ?> Link to comment https://forums.phpfreaks.com/topic/101335-flat-file-question/#findComment-518878 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.