jimboppin Posted February 25, 2017 Share Posted February 25, 2017 (edited) hi there, im making a media page on my static site that has no database, i have managed to put this code together so that i can add text files into a directory and show all files content on a page.. my problem is i cant work out how to get it all in order, it seems that by default the order is alphabetical and i need it to be by the last modified date... any help would be great as im a bit stuck my function: function get_media($media_dir) { $directory = PAGES_BASEDIR.$media_dir; $dir = opendir($directory); while (($file = readdir($dir)) !== false) { $filename = $directory . $file; $type = filetype($filename); if ($type == 'file') { $contents = file_get_contents($filename); $items = explode('¬', $contents); $media_title = basename($filename); $media_title = basename($filename, ".txt"); if (file_exists($filename)) { $date_time = date ("F d Y H:i:s", filemtime($filename)); } foreach ($items as $media) { THEMENEWSTOP($media_title); echo ($media); THEMENEWSBOTTOM($date_time); } } } closedir($dir); } Thanks Edited February 25, 2017 by jimboppin Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/ Share on other sites More sharing options...
mac_gyver Posted February 25, 2017 Share Posted February 25, 2017 readdir() returns the entries in the order that they are stored in the directory. to do what you want will require sorting by the raw filemtime() value. to do this, you would store the filenames and filemtime in an array, with the filename as the array index/key and the filemtime as the array value. you would then use either asort() or arsort() to sort the data by the filemtime values, then loop over the sorted data to display the information. i also recommend just using glob(), rather than all that opendir/readdir/filetype logic, to get the starting list of the files into an array. Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543368 Share on other sites More sharing options...
jimboppin Posted February 25, 2017 Author Share Posted February 25, 2017 ok cool ill do my best to try and put all that together and switch to glob, thankyou. Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543369 Share on other sites More sharing options...
Barand Posted February 25, 2017 Share Posted February 25, 2017 You may find it better to store the date and contents in the array (with filename as the key). In which case a simple asort() will not work, you will need a custom sort function. For example $files['file1.txt'] = [ 'date' => '2017-01-03', 'content' => 'aaa' ]; $files['file2.txt'] = [ 'date' => '2017-01-01', 'content' => 'bbb' ]; $files['file3.txt'] = [ 'date' => '2017-01-02', 'content' => 'ccc' ]; $files['file4.txt'] = [ 'date' => '2017-01-04', 'content' => 'ddd' ]; uasort($files, function ($a, $b) { return strcmp ($a['date'], $b['date']); }); foreach ($files as $filename => $fdata) { // process the output } Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543370 Share on other sites More sharing options...
jimboppin Posted February 25, 2017 Author Share Posted February 25, 2017 hi, here is what i have managed to come up with and this is working. however is it valid and clean? function get_media($media_dir) { $directory = PAGES_BASEDIR.$media_dir; $files = array(); $files = glob($directory."*.txt"); usort($files, function($x, $y) { return filemtime($x) < filemtime($y); }); foreach ($files as $media) { $media_title = basename($media); $media_title = basename($media, ".txt"); if (file_exists($media)) { $date_time = date ("F d Y H:i:s", filemtime($media)); } THEMENEWSTOP($media_title); require_once $media; THEMENEWSBOTTOM($date_time); } } thanks Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543378 Share on other sites More sharing options...
Barand Posted February 25, 2017 Share Posted February 25, 2017 A sort function should return -ve, zero or +ve values depending on the comparison result. Yours will return a boolean result. This would achieve that return filemtime($x) - filemtime($y); Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543379 Share on other sites More sharing options...
Jacques1 Posted February 25, 2017 Share Posted February 25, 2017 Including a text file with require_once is suicidal, because this turns the text into code which gets executed. Anybody with expected or unexpected access to those files can execute arbitrary commands on your server. Even echoing the content will make the application wide open to cross-site scripting attacks. If you want plain text, you need to escape the file content: $content = file_get_contents(...); echo htmlspecialchars($content, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); You should definitely reconsider your application infrastructure. I know that flat-file application are all the rage right now, but implementing this correctly -- i. e. without blowing up your server -- is a lot more work than simply using a proper database. Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543381 Share on other sites More sharing options...
jimboppin Posted February 25, 2017 Author Share Posted February 25, 2017 hi thankyou for the advice, its just a small project im working on, theres no edit or delete funtionality going into it... i have implemented what you suggested here function get_media($media_dir) { $directory = PAGES_BASEDIR.$media_dir; $files = array(); $files = glob($directory."*.txt"); usort($files, function($x, $y) { return filemtime($x) < filemtime($y); }); foreach ($files as $media) { $content = file_get_contents($media); $media_title = basename($media); $media_title = basename($media, ".txt"); if (file_exists($media)) { $date_time = date ("F d Y H:i:s", filemtime($media)); } THEMEMEDIATOP($media_title); echo htmlspecialchars($content, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); THEMEMEDIABOTTOM($date_time); } } but its showing up the html in the page now is there a way to auto apply the html so i can just create a text document from within windows and just upload it no worries... i changed this return filemtime($x) - filemtime($y); but it just changed it back to ABC and not last date modified... thanks Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543384 Share on other sites More sharing options...
Jacques1 Posted February 25, 2017 Share Posted February 25, 2017 (edited) but its showing up the html in the page now is there a way to auto apply the html so i can just create a text document from within windows and just upload it no worries... So your “text files” are actually HTML documents? Then you should stop pretending like they're text files. Give them a proper “.html” extension, double-check the file permissions (they must be read-only), then echo the raw content. Edited February 25, 2017 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543385 Share on other sites More sharing options...
jimboppin Posted February 25, 2017 Author Share Posted February 25, 2017 not realy, i didont look at it like that... i wanted to use a txt file just because there just articles that consist of <br /> etc nothing big... could i not whitelist some of these ? commonly used html tags? and the server im on is windows so im not sure how the file permissions work... i only see black blocks and xxx in filezilla rather then 755 etc.. Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543388 Share on other sites More sharing options...
Jacques1 Posted February 25, 2017 Share Posted February 25, 2017 not realy, i didont look at it like that... i wanted to use a txt file just because there just articles that consist of <br /> etc nothing big Sounds like you want Markdown rather than HTML. and the server im on is windows so im not sure how the file permissions work... i only see black blocks and xxx in filezilla rather then 755 etc.. Then ask the person who maintains the server. If that's you, look up how the permissions work, because that's kinda important. Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543390 Share on other sites More sharing options...
jimboppin Posted February 25, 2017 Author Share Posted February 25, 2017 cool, ill look into them thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543391 Share on other sites More sharing options...
jimboppin Posted February 25, 2017 Author Share Posted February 25, 2017 hi, i have decided to change my files to html and changed the code, is this safe? function get_media($media_dir) { $directory = PAGES_BASEDIR.$media_dir; $files = array(); $files = glob($directory.'*.html'); usort($files, function($x, $y) { return filemtime($x) < filemtime($y); }); foreach ($files as $media) { $content = file_get_contents($media); $clean_content = htmlspecialchars($content, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); $clean_content = strip_tags($content, '<br><p><a><img><h1><h2><h3><h4>'); $media_title = basename($media); $media_title = basename($media, '.html'); if (file_exists($media)) { $date_time = date ('F d Y H:i:s', filemtime($media)); } THEMEMEDIATOP($media_title); echo $clean_content; THEMEMEDIABOTTOM($date_time); } } thanks Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543399 Share on other sites More sharing options...
jimboppin Posted February 25, 2017 Author Share Posted February 25, 2017 there is a .htaccess file in all directories aswell denying all access the only place i found i cant use them is in my css directory Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543400 Share on other sites More sharing options...
Jacques1 Posted February 26, 2017 Share Posted February 26, 2017 is this safe? Given that strip_tags() is the worst possible choice and that you're struggling with the file permissions: no. So what happened with Markdown? If you want to filter your HTML in addition to or instead of using Markdown, there's HTML Purifier. The strip_tags() function is not an option for anything but joke applications, as the manual clearly says. Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543401 Share on other sites More sharing options...
jimboppin Posted February 26, 2017 Author Share Posted February 26, 2017 markdown is here i hope iv got it right function get_media($media_cat) { global $Parsedown; if (isset($_GET['id'])) { $media_id = htmlentities($_GET['id'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); } if (isset($media_id)) { if ($media_id == $media_cat) { $media_dir = 'Media/'.$media_cat.'/'; $directory = PAGES_BASEDIR.$media_dir; $files = array(); $files = glob($directory.'*.txt'); usort($files, function($x, $y) { return filemtime($x) < filemtime($y); }); foreach ($files as $media) { $content = file_get_contents($media); $media_title = basename($media); $media_title = basename($media, '.txt'); if (file_exists($media)) { $date_time = date ('F d Y H:i:s', filemtime($media)); } THEMEMEDIATOP($media_title); echo $Parsedown->text($content); THEMEMEDIABOTTOM($date_time); } } } } also can you point me in the right direction for windows security regarding the files... Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543408 Share on other sites More sharing options...
Jacques1 Posted February 26, 2017 Share Posted February 26, 2017 You need $Parsedown->setMarkupEscaped(true); to disable embedded HTML markup. also can you point me in the right direction for windows security regarding the files... I already did that: Then ask the person who maintains the server. If that's you, look up how the permissions work, because that's kinda important. I neither use Windows nor have access to your server, so I cannot fix the permissions for you. Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543409 Share on other sites More sharing options...
jimboppin Posted February 26, 2017 Author Share Posted February 26, 2017 i have this in my mainfile require_once INCLUDES_BASEDIR.'_parsedown.php'; $Parsedown = new Parsedown(); $Parsedown->setMarkupEscaped(true); and this is another update on my function to collect news or announcements etc function get_media($media_cat, $display_recent) { global $Parsedown; if (isset($_GET['id'])) { $media_id = htmlentities($_GET['id'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); } if (isset($media_id)) { if ($media_id == $media_cat) { $display_recent = null; } } $media_dir = 'Media/'.$media_cat.'/'; $directory = PAGES_BASEDIR.$media_dir; $files = array(); $files = glob($directory.'*.txt'); usort($files, function($x, $y) { return filemtime($x) < filemtime($y); }); $recent_content = array_slice($files, 0, $display_recent); foreach ($recent_content as $media) { $content = file_get_contents($media); $media_title = basename($media); $media_title = basename($media, '.txt'); if (file_exists($media)) { $date_time = date ('F d Y H:i:s', filemtime($media)); } THEMEMEDIATOP($media_title); echo $Parsedown->text($content); THEMEMEDIABOTTOM($date_time); } } then i just add this into a page i want to show my content <?php get_media('News', 2);?> other then the permissions is this all ok now? i will look into the permissions thanks Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543410 Share on other sites More sharing options...
jimboppin Posted February 27, 2017 Author Share Posted February 27, 2017 You may find it better to store the date and contents in the array (with filename as the key). In which case a simple asort() will not work, you will need a custom sort function. For example $files['file1.txt'] = [ 'date' => '2017-01-03', 'content' => 'aaa' ]; $files['file2.txt'] = [ 'date' => '2017-01-01', 'content' => 'bbb' ]; $files['file3.txt'] = [ 'date' => '2017-01-02', 'content' => 'ccc' ]; $files['file4.txt'] = [ 'date' => '2017-01-04', 'content' => 'ddd' ]; uasort($files, function ($a, $b) { return strcmp ($a['date'], $b['date']); }); foreach ($files as $filename => $fdata) { // process the output } hi im trying to implement this to my code as i found out that if i backup my site then upload it all my news is reset and gets mixed up.. how do i format my txt file so this can read it and sort it out? i imagin somthing like this... date>27/02/2017 6:48pm anything after the above line is content some contentsome contentsomesome contentsome some contentsome contentsome some contentsome content some contentsome content some contentsome content some contentsome contentsome content heres the function... function get_media($media_cat, $display_recent) { global $Parsedown, $media_id; if (isset($media_id)) { if ($media_id == $media_cat) { $display_recent = null; } } $directory = MEDIA_BASEDIR.$media_cat.'/'; $files = array(); $files = glob($directory.'*.txt'); uasort($files, function ($a, $b) { return strcmp ($a['date'], $b['date']); }); $recent_content = array_slice($files, 0, $display_recent); foreach ($recent_content as $media => $fdata) { $content = file_get_contents($fdata); $media_title = basename($fdata); $media_title = basename($fdata, '.txt'); if (file_exists($fdata)) { $date_time = date ('F d Y H:i:s', filectime($fdata)); } THEMEMEDIATOP($media_title, $media_cat); echo $Parsedown->text($content); THEMEMEDIABOTTOM($date_time); } } thanks again Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543459 Share on other sites More sharing options...
jimboppin Posted February 27, 2017 Author Share Posted February 27, 2017 sorry i see how that works now, i was hopeing to be able to just upload a txt file no worries, and to do that the only thing i can think of is to put the date and time in the first line of the text file and then read the first line to sort by date first line being line[0] i know how to read the first line im just stuck how to use it in the uasort function thanks Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543472 Share on other sites More sharing options...
Jacques1 Posted February 28, 2017 Share Posted February 28, 2017 Just use a database already. You've now spent three days on this trivial task, you constantly get stuck, and it will be even worse when you have to do complex tasks. So why continue this route? The entire thread could be boiled down to a single SQL query: $media = $databaseConnection->query(' SELECT title, content, category, creation_time FROM media ORDER BY creation_time DESC -- add LIMIT if needed '); foreach ($media as $medium) { // parse and display $medium } That's it. No glob(), no uasort(), no array_slice(), no filectime(), no file_get_contents(). Just standard SQL. Even publishing will be simpler, because you don't have to upload anything. Make yourself a simple password-protected admin script and use that to manage your texts. Maybe the server already has a MySQL installation. If it doesn't, you can always use SQLite which stores the entire database in a single file. Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543483 Share on other sites More sharing options...
jimboppin Posted February 28, 2017 Author Share Posted February 28, 2017 Hi, thankyou for reply, i do have sqli, i was on that track but i couldont work out how to get it secure when posting username and password etc i dont want my username or password sniffed out when its going thru to the script, i honestly would need help to get me going on somthing like that, i would need a secure basic sql login and then how to post to the database securley when adding data to it... this is what made me try and make it more simple because i live in notepad++ and filezilla it just made sence to me. im willing to learn if some one is willing to help me get going with it i can self teach the rest but security makes me on edge Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543514 Share on other sites More sharing options...
jimboppin Posted February 28, 2017 Author Share Posted February 28, 2017 also tutorials and examples are out of date that i found... Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543515 Share on other sites More sharing options...
Jacques1 Posted February 28, 2017 Share Posted February 28, 2017 Apache has built-in support for password authentication. When you use HTTPS, the bcrypt hash algorithm and a decent password, this is reasonably secure. Make sure to prevent cross-site request forgery attacks with Double Submit Cookies. If you have the PDO database extension, read this great tutorial. For mysqli, read that one. Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543518 Share on other sites More sharing options...
jimboppin Posted February 28, 2017 Author Share Posted February 28, 2017 (edited) thanks i know its alot better to have a database but its alot more work when i can just make a little function to collect a text file and its contents, also the speed difference sql and txt files is just stupidly fast so for a site that dose not require users or admin i would much rather stick to this function. sorry to take 2 steps back, if you cant or dont want to help me thats fine i can seek help some where else, no affence. i just need to read the first line that will have a date format on it.. read it and arange by that date from the top line then output while ignoring the first line for content. thanks again. Edited February 28, 2017 by jimboppin Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/#findComment-1543519 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.