Every first time I create a .txt on my server using a Form, it writes like a charm, but when I add a new line in the .txt using also the Form, and view the .txt from a simple file browser made by PHPToys the new line in the .txt cannot be viewed, the old line is still there but the new line does not add up, but when i open the .txt on a notepad, the new line is there, what is the code to make the .txt updated or some kind of reload or refresh so that the new added line can be seen on the .txt using a file browser.

here is my file browser code:

<?php
function showContent($path){
$path = "records/";
   if ($handle = opendir($path))
   {
       $up = substr($path, 0, (strrpos(dirname($path."/."),"/")));
       

       while (false !== ($file = readdir($handle)))
       {
           if ($file != "." && $file != "..")
           {
               $fName = $file;
               $file = $path.'/'.$file;
               if(is_file($file)) {
                   echo "<tr><td><img src='style/file2.gif' width='16' height='16' alt='file'/> <a href='".$file."'>".$fName."</a></td>"
                            ."<td align='right'>".date ('d-m-Y H:i:s', filemtime($file))."</td>"
                            ."<td align='right'>".filesize($file)." bytes</td></tr>";
               } elseif (is_dir($file)) {
                   print "<tr><td colspan='2'><img src='style/dir2.gif' width='16' height='16' alt='dir'/> <a href='".$_SERVER['PHP_SELF']."?path=$file'>$fName</a></td></tr>";
               }
           }
       }

       closedir($handle);
   }	

}

if (isset($_POST['submitBtn'])){
	$actpath = isset($_POST['path']) ? $_POST['path'] : '';	
} else {
	$actpath = isset($_GET['path']) ? $_GET['path'] : '';	
}


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
   <link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="main">
     

      <div class="caption">Records: <?php echo $actpath ?></div>
      <div id="icon2">&nbsp;</div>
      <div id="result">
        <table width="100%">
<?php
			showContent($actpath);        
?>
        </table>
     </div>
    </div>
</body>

Dani AI

Generated

The behaviour described by (Notepad shows the new line but the browser file view does not) strongly points to caching — either the browser cache or an intermediate cache returning an older copy. was correct to point at response headers; ’s cache-busting idea (changing the URL) is also a practical quick fix. The missing piece in the thread is a safe, repeatable implementation and a short checklist for diagnosis.

A robust approach is to stop serving the .txt files directly and instead stream them through a small PHP wrapper that sets explicit no-cache headers and sanitizes the requested filename. This forces the client to request fresh content and avoids accidental directory-traversal. Example (wrap access in a script called e.g. viewtext.php):

<?php
$root = __DIR__ . '/records/';
$name = basename($_GET['f']);           // prevents ../ attacks
$path = $root . $name;
if (!is_file($path)) { http_response_code(404); exit; }

header('Content-Type: text/plain; charset=utf-8');
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');

readfile($path);
exit;
?>

If changing delivery is not desired, append a deterministic version token to the file URL when generating the file list (for example based on the file modification time). That makes the URL change after each write so browsers fetch the new resource instead of using a cached copy. Also verify caching layer(s) by requesting headers (e.g. with curl -I) or inspecting the Network tab in devtools to see Cache-Control, Expires, Last-Modified and response codes.

Extra notes: check for upstream caches or CDNs, ensure server time and filemtime are updated, and always validate any file input on the server side to avoid exposing arbitrary files. More on HTTP cache headers: Cache-Control — MDN.

Recommended Answers

All 4 Replies

This may just be the caching of your browser.

yes but any codes to use? how to prevent browser from caching the .txt file.... Thanks for the reply.

Turn off caching, start reading here. Although it may not always work.

Together with pritaeas suggest, you can also append a random string to the end of the file:

<a href="/path/file.txt?random_string">file name</a>

you won't need that value to open the file and the browser will read always a new link.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.