Jump to content

Flat File Question


voyde

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.