Jump to content

How to prevent txt files from getting cached?


Krystal

Recommended Posts

Hi guys. 

 

I'm a very very noob at html and I have something like this:

 

<script type="text/javascript">
window.onload=function () {
var objDivlog = document.getElementById("txt");
objDivlog.scrollTop = objDivlog.scrollHeight;
}
</script>
 
</head>
<body>
 
<table height="100%" border="0">
<tr>
<td width="120px" align="left" valign="top">
<div class="list">
<?php
$files = array();
$dir = opendir('logs');
while(false != ($file = readdir($dir))) {
        if(strpos($file, '.txt') !== FALSE) {
                $files[] = $file;
        }   
}
 
natsort($files);
$files = array_reverse($files);
 
foreach($files as $file) {
        echo '
                <a href="#" onclick="$(\'#logfile\').load(\'logs/'.$file.'\');">'.$file.'</a><br>';
}
?>
 

I constantly change the txt files but once it's loaded, it won't change next time.

Is there a way to prevent txt files from getting cached?

 

Thanks!

Link to comment
Share on other sites

It's simply appending a ?number to the end of the urls

In js you can use math.random() and in php I used mt_rand();

Is many ways to create a random number.

 

Some changes.

If you want to add additional scroll to top or to positions can make a new js function to do that and include the window.open as well.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Files</title>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
<style>
.listfiles{
display:block;
position:relative;    
width:120px;
margin:0;
padding:0;
text-decoration:none;
}
</style>
</head>
<body>

<div>
<?php
$files = array();
$dir   = opendir('logs');
while (false != ($file = readdir($dir))) {
    if (strpos($file, '.txt') !== FALSE) {
        $files[] = $file;
    }
}

natsort($files);
$files = array_reverse($files);
foreach ($files as $file) {
    $random   = mt_rand(1, 99999999);
    $location = "http://" . $_SERVER['SERVER_NAME'] . "/logs/$file?$random";
    echo "<a class='listfiles' href='$location' onclick='window.open('$location', '_blank');'>$file</a>";
}

?>
</div>
</body>
</html>
Edited by QuickOldCar
Link to comment
Share on other sites

I use filemtime() to get the files last modified date to use for a timestamp. Then whenever the file gets updated, it automatically bumps the timestamp and forces the browser to download the new file, otherwise it will download it whether it was updated or not, which is kind of a waste.

$file = '/home/user/public_html/assets/css/my_css.css';  //path to raw file
$asset = '/assets/css/my_css.css'; //to be used for asset link
 
//If the file existed, grab it's last modified time and append it to the asset for cache-busting purposes
if (file_exists($file)) {
 $asset .= '?v=' . date ('ymdHis', filemtime($file));
}
 
echo $asset; //something like '/assets/css/my_css.css?v=150815090530'
Edited by CroNiX
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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