Krystal Posted August 15, 2015 Share Posted August 15, 2015 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! Quote Link to comment https://forums.phpfreaks.com/topic/297796-how-to-prevent-txt-files-from-getting-cached/ Share on other sites More sharing options...
scootstah Posted August 15, 2015 Share Posted August 15, 2015 A quick hack is to append a timestamp to the URL. So instead of http://example.com/logs.txt, you have http://example.com/logs.txt?123456789 Quote Link to comment https://forums.phpfreaks.com/topic/297796-how-to-prevent-txt-files-from-getting-cached/#findComment-1518874 Share on other sites More sharing options...
Krystal Posted August 15, 2015 Author Share Posted August 15, 2015 A quick hack is to append a timestamp to the URL. So instead of http://example.com/logs.txt, you have http://example.com/logs.txt?123456789 Can you show me an example applied to the codes i posted? Quote Link to comment https://forums.phpfreaks.com/topic/297796-how-to-prevent-txt-files-from-getting-cached/#findComment-1518875 Share on other sites More sharing options...
Krystal Posted August 15, 2015 Author Share Posted August 15, 2015 If anyone can show me an example applied on posts i put instead of explanation, that would be really great and helpful. Quote Link to comment https://forums.phpfreaks.com/topic/297796-how-to-prevent-txt-files-from-getting-cached/#findComment-1518876 Share on other sites More sharing options...
gizmola Posted August 15, 2015 Share Posted August 15, 2015 I think you need to elaborate on your issue. Is it that you are adding/removing files and the list on your page is not being updated? or The contents of the individual files are not being updated? Quote Link to comment https://forums.phpfreaks.com/topic/297796-how-to-prevent-txt-files-from-getting-cached/#findComment-1518879 Share on other sites More sharing options...
QuickOldCar Posted August 15, 2015 Share Posted August 15, 2015 (edited) 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 August 15, 2015 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/297796-how-to-prevent-txt-files-from-getting-cached/#findComment-1518886 Share on other sites More sharing options...
CroNiX Posted August 15, 2015 Share Posted August 15, 2015 (edited) 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 August 15, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/297796-how-to-prevent-txt-files-from-getting-cached/#findComment-1518936 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.