ztealmax Posted December 1, 2006 Share Posted December 1, 2006 Hi i small question or big ;)I wish to scan a folder for text files, and show textfiles on mainpage one at the time, begining with the newest fileedited, is this possible? (Maybe get files with edit dates and time? and there after display latest file)i use this script for showing all files in a folder[sup]<? if ($dir = @opendir("news/")) { while (($file = readdir($dir)) !== false) { if($file != ".." && $file != ".") { $filelist[] = $file; } } closedir($dir); } ?><form> <select name="selected_dir" > <?php asort($filelist); while (list ($key, $val) = each ($filelist)) { echo "<option>$val</option>"; } ?> </select></form> [/sup] Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/ Share on other sites More sharing options...
taith Posted December 1, 2006 Share Posted December 1, 2006 just as a suggestion... its ALOT faster to use glob() to get files :-)[code]<?$filelist=glob("news/*.txt");?>[/code] Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133452 Share on other sites More sharing options...
ztealmax Posted December 1, 2006 Author Share Posted December 1, 2006 [quote author=taith link=topic=116995.msg477020#msg477020 date=1164987641]just as a suggestion... its ALOT faster to use glob() to get files :-)[code]<?$filelist=glob("news/*.txt");?>[/code][/quote]Thanx , yeaa just tried it mush better :P im a bit of a noob ;) anyway to open files to read files starting with the newest first, and set others as number links?BTW im not using sql ;) so you all know :p:D Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133457 Share on other sites More sharing options...
keeB Posted December 1, 2006 Share Posted December 1, 2006 You can use the stat function.PLenty of examples: http://us2.php.net/manual/en/function.stat.php:)If you need any more help, just let me know! Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133472 Share on other sites More sharing options...
taith Posted December 1, 2006 Share Posted December 1, 2006 heres how you get the time of when the file was made[code]date("F d Y H:i:s", filemtime($file));[/code] Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133473 Share on other sites More sharing options...
ztealmax Posted December 1, 2006 Author Share Posted December 1, 2006 [b]keeB[/b] checked it out, cant seem to find what im after, but maybe im just missing it! Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133490 Share on other sites More sharing options...
ztealmax Posted December 1, 2006 Author Share Posted December 1, 2006 how do i make it so it reads the latest edited file in that folder?i meen like it scans all files in the folder then opens the latest file for viewing, and after that it sort the rest of the files in date order as number links for ex:[img]http://cms.indivi.se/filesread.png[/img]Yea iknow im a noob ;)thinking of trying to smash codes togheter got these right now:scanfolder.php[code]<?$filelist=glob("news/*.txt");?> <form> <select name="selected_dir" > <?php asort($filelist); while (list ($key, $val) = each ($filelist)) { echo "<option>$val</option>"; } ?> </select></form> [/code]read.php[code]<?php$filename = 'news/news.txt';if (file_exists($filename)) { echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));}// set file to read$file = 'news/news.txt';// open file $fh = fopen($file, 'r') /*or die('Could not open file!')*/;// read file contents$data = fread($fh, filesize($file)) /*or die('Could not read file!')*/;// close file fclose($fh); // print file contents echo $data; ?>[/code]As you can see i can only open one file in this read script, and that is what i tell it to openin this example i use [i]news.txt[/i]Just not sure how im suppose to do it ;) Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133520 Share on other sites More sharing options...
keeB Posted December 1, 2006 Share Posted December 1, 2006 [code=php:0]<?php// outputs e.g. somefile.txt was last modified: December 29 2002 22:16:23.$filename = 'somefile.txt';if (file_exists($filename)) { echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));}?> [/code] Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133526 Share on other sites More sharing options...
ztealmax Posted December 1, 2006 Author Share Posted December 1, 2006 yea i got it to tell me what date is was latest modified, what im after is that it opens the file for view/read from the folder news/ and only the file that was last modified / edited and places all other content as links as i demostrated on pic above :)Im sorry if im not clear to what i meen , my english isnt that good :) Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133528 Share on other sites More sharing options...
taith Posted December 1, 2006 Share Posted December 1, 2006 oy... enough of this... improvising time![code]<?if($dir = @opendir("news/")){ while(($file = readdir($dir)) !== false){ if($file != ".." && $file != "."){ $date=date ("YmHis.", filemtime($filename)); $filelist['$date'] = $file; } } closedir($dir);}?><form><select name="selected_dir" ><?php ksort($filelist);while(list ($key, $val) = each ($filelist)){ echo "<option>$val</option>";}?></select></form> [/code]hows that work? you might need a different sort... Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133538 Share on other sites More sharing options...
ztealmax Posted December 1, 2006 Author Share Posted December 1, 2006 [quote author=taith link=topic=116995.msg477108#msg477108 date=1164994911]oy... enough of this... improvising time![code]<?if($dir = @opendir("news/")){ while(($file = readdir($dir)) !== false){ if($file != ".." && $file != "."){ $date=date ("YmHis.", filemtime($filename)); $filelist['$date'] = $file; } } closedir($dir);}?><form><select name="selected_dir" ><?php ksort($filelist);while(list ($key, $val) = each ($filelist)){ echo "<option>$val</option>";}?></select></form> [/code]hows that work? you might need a different sort...[/quote]it work as such as it only showed file ending with 4 / news4.txthttp://cms.indivi.se/test.php as you can see here :)[img]http://cms.indivi.se/filesread2.png[/img]Im sorry if im such a no0b :D Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133542 Share on other sites More sharing options...
taith Posted December 1, 2006 Share Posted December 1, 2006 i did say there may need a different sort(asort,ksort...)to further it along...[code]<?if($dir = @opendir("news/")){ while(($file = readdir($dir)) !== false){ if($file != ".." && $file != "."){ $i++; $k=str_pad($i,7, "0",STR_PAD_LEFT); $date=date ("YmHis.", filemtime($filename)); $filelist['$date'] = $file; } } closedir($dir);}?><form><select name="selected_dir" ><?php ksort($filelist);while(list ($key, $val) = each ($filelist)){ echo "<option>$val</option>";}?></select></form>[/code] Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133549 Share on other sites More sharing options...
ztealmax Posted December 1, 2006 Author Share Posted December 1, 2006 Thanx sorry my english isnt that good ;)im testing now:http://cms.indivi.se/test.phpok tested it now, it worked :) but i had to rename files so they dont contain any numbers in the names, when i did that the date thingy worked great :Dthanx m8Now how do i sort the remaining files as a text link [1][2].....You got any idea? :D Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133556 Share on other sites More sharing options...
ztealmax Posted December 1, 2006 Author Share Posted December 1, 2006 hmm no it didnt work all of a sudden...weirdit must sort by name or something! Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133563 Share on other sites More sharing options...
taith Posted December 1, 2006 Share Posted December 1, 2006 [code]<?if($dir = @opendir("news/")){ $i=0; while(($file = readdir($dir)) !== false){ if($file != ".." && $file != "."){ $i++; $k=str_pad($i,7, "0",STR_PAD_LEFT); $date=date("YmHis", filemtime($file)).$k; $filelist[$date] = $file; } } closedir($dir);}?><form><select name="selected_dir" ><?krsort($filelist);while(list($key, $val) = each($filelist)){ echo "<option>$val</option>";}?></select></form>[/code] Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133569 Share on other sites More sharing options...
ztealmax Posted December 1, 2006 Author Share Posted December 1, 2006 Im sorry im just not getting it to work :(it stills seems to sort by filenamebtw can i somehow change:[code]<form><select name="selected_dir" ><?krsort($filelist);while(list($key, $val) = each($filelist)){ echo "<option>$val</option>";}?></select></form>[/code]to have this instead:[code]<?php$filename = 'news/news.txt';if (file_exists($filename)) { echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));}// set file to read$file = 'news/news.txt';// open file $fh = fopen($file, 'r') /*or die('Could not open file!')*/;// read file contents$data = fread($fh, filesize($file)) /*or die('Could not read file!')*/;// close file fclose($fh); // print file contents echo $data; ?>[/code]ofcource filename equal what the date tells it should be $val i guess? Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133571 Share on other sites More sharing options...
taith Posted December 1, 2006 Share Posted December 1, 2006 no... on that webpage, its sorting by reverse key... xtx-->ztx-->xt that should be right... no? if its backwords... change krsort to ksort... Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133572 Share on other sites More sharing options...
ztealmax Posted December 1, 2006 Author Share Posted December 1, 2006 [quote author=taith link=topic=116995.msg477142#msg477142 date=1164998183]no... on that webpage, its sorting by reverse key... xtx-->ztx-->xt that should be right... no? if its backwords... change krsort to ksort...[/quote]ohh ok, i thought we where trying to only show the file last modified? ;) "check it by date then show that file" Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133575 Share on other sites More sharing options...
taith Posted December 1, 2006 Share Posted December 1, 2006 with that code... you cant resort it down again... just put a limiter on how many it shows...[code]<?if($dir = @opendir("news/")){ $i=0; while(($file = readdir($dir)) !== false){ if($file != ".." && $file != "."){ $i++; $k=str_pad($i,7, "0",STR_PAD_LEFT); $date=date("YmHis", filemtime($file)).$k; $filelist[$date] = $file; } } closedir($dir);}krsort($filelist);while(list($key, $val) = each($filelist)){ if(!isset($p)){ $p=x; echo $val; }}?>[/code] Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133580 Share on other sites More sharing options...
ztealmax Posted December 1, 2006 Author Share Posted December 1, 2006 [quote author=taith link=topic=116995.msg477150#msg477150 date=1164999046]with that code... you cant resort it down again... just put a limiter on how many it shows...[code]<?if($dir = @opendir("news/")){ $i=0; while(($file = readdir($dir)) !== false){ if($file != ".." && $file != "."){ $i++; $k=str_pad($i,7, "0",STR_PAD_LEFT); $date=date("YmHis", filemtime($file)).$k; $filelist[$date] = $file; } } closedir($dir);}krsort($filelist);while(list($key, $val) = each($filelist)){ if(!isset($p)){ $p=x; echo $val; }}?>[/code][/quote]ahh okej... Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133586 Share on other sites More sharing options...
ztealmax Posted December 2, 2006 Author Share Posted December 2, 2006 no one knows how to do this? ::) Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-133984 Share on other sites More sharing options...
ztealmax Posted December 3, 2006 Author Share Posted December 3, 2006 hmm ok, but what i really are after is for it to open and read the file that was last modified in the folder!for example are there 3 files:file.txt modified 2006-12-01 22:00test.txt modified 2006-12-01 22:05gurka.txt modified 2006-12-01 22:10So what i need the script to do is open file names gurka.txt as that is the one last modified Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-134168 Share on other sites More sharing options...
ztealmax Posted December 5, 2006 Author Share Posted December 5, 2006 Guys i really need help with getting it to read the last file edited or createdDoesnt anybody know exactly how to do this? ;)//Cheers Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-135282 Share on other sites More sharing options...
ztealmax Posted December 9, 2006 Author Share Posted December 9, 2006 Now i have the date thingy workingThis gives me a list of all files in my folder news/and sets the latest file edited on the first row:[code]<?php $this = $PHP_SELF; $dir = $DOCUMENT_ROOT."news/"; $files = opendir($dir); $file_list = array(); $file_sort = array(); if(empty($sort))$sort="name"; if(empty($r)) $r=0; $cnt = 1; while ($file = readdir($files)) { $full_path = $dir."/".$file; if(!is_dir($full_path)) { $ext = explode(".",$file); $i=count($ext); if($i>1)$file_details["ext"] = strtolower($ext[$i-1]); $file_details["name"] = $ext[0]; $file_details["date"] = filectime($full_path); $file_list[$cnt] = $file_details; $key = strtolower($file_details[$sort]); $file_sort[$cnt] = $key; $cnt++; } } if($r)arsort($file_sort); else asort($file_sort); ?><link href="../webmax.css" rel="stylesheet" type="text/css"><table width="465" border="0" cellpadding="3" cellspacing="0" class="bodytext"> <tr> <td width="174"><a href="<?php print($this);?>?sort=name&r=<?php print(!$r);?>">Name</a></td> <td width="141"><a href="<?php print($this);?>?sort=date&r=<?php print(!$r);?>">Date</a></td> </tr> <tr bgcolor="#0033CC"> <td height="3" colspan="5"> </td> </tr><?php while(list($key, $value)=each($file_sort)){ $value = $file_list[$key];?> <tr> <td width="174"><?php print($value["name"]);?></td> <td width="141"><?php print(date("Y/m/d H:i",$value["date"]));?></td> </tr><?php}?></table>[/code]Now for a follow up questionHow can i now read only that file (latest modified) with this code (needs modification):[code]<?php$filename = 'news/*.*';// set file to read$file = 'news/info.txt';// open file $fh = fopen($file, 'r') /*or die('Could not open file!')*/;// read file contents$data = fread($fh, filesize($file)) /*or die('Could not read file!')*/;// close file fclose($fh); // print file contents echo $data; ?>[/code] Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-137949 Share on other sites More sharing options...
ztealmax Posted December 9, 2006 Author Share Posted December 9, 2006 Guess no one knows what the hell im talking about ;) Link to comment https://forums.phpfreaks.com/topic/29118-view-files-on-mainpage-from-a-folder-starting-with-the-newest-text-file-edited/#findComment-138210 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.