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] Quote Link to comment 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] Quote Link to comment 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 Quote Link to comment 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! Quote Link to comment 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] Quote Link to comment 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! Quote Link to comment 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 ;) Quote Link to comment 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] Quote Link to comment 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 :) Quote Link to comment 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... Quote Link to comment 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 Quote Link to comment 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] Quote Link to comment 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 Quote Link to comment 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! Quote Link to comment 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] Quote Link to comment 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? Quote Link to comment 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... Quote Link to comment 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" Quote Link to comment 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] Quote Link to comment 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... Quote Link to comment 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? ::) Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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] Quote Link to comment 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 ;) Quote Link to comment 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.